i've defined an initial Table with three rows and 9 Column in Datagrid. right now button should be visible only if i select a row and then press another button that i defined in my Ribbon-Tab after that my Button will be Visible. sofar everything works well, but the Problem is after saving my Table, closing it and open the Table again the button is not there anymore. I set the Visibility based on if the DataGridCell.IsSelected, also a BooleanToVisibilityConverter to convert the boolean value to a Visibility one.
can anyone help!
XAML:
<DataGrid.Resources>
<BooleanToVisibilityConverter x:Key="BoolToVisConverter" />
</DataGrid.Resources>
<DataGridTemplateColumn x:Name="subgraphtyp" Header="H." Width="50">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Name="btnTable" Visibility="{Binding Path=Hinterlegung, Converter=
{StaticResource BoolToVisConverter}}" Height="20" Width="25"
Click="Button_Table_Click">
<Image Height="16" Source="Subgraph.png" Stretch="Fill" Width="16"/>
</Button>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
C#:
public bool Hinterlegung { get; set; }
private void Button_StartTableModus(object sender, RoutedEventArgs e)
{
if (DataGrid1.SelectedItem != null && tabItem1.IsSelected)
{
TableDataRowStringItem item = (TableDataRowStringItem)DataGrid1.CurrentItem;
string wert = item.ObjectType;
string rowName = item.Name;
if (wert == "Function" || wert == "Process")
{
item.Hinterlegung = true;
if (!tabControl.Items.Contains(tabItem2))
{
tabControl.Items.Add(tabItem2);
tabItem2.Focus();
tabItem2.IsSelected = true;
tabItem2.Header = rowName;
TableTab.Visibility = Visibility.Visible;
openTabs++;
DataGrid2.IsReadOnly = false;
starting_Table_Mod_at_start2V();
}
}
}
}
//this my initial Table
private ObservableCollection<TableDataRowStringItem> tableobject = new
ObservableCollection<TableDataRowStringItem>();
private void starting_Table_Mod_at_start2V()
{
List<TableDataRowStringItem> rowstringList = new List<TableDataRowStringItem>();
TableDataRowStringItem item = new TableDataRowStringItem();
item.RowNumber = 1; item.saveFlag = true; item.ObjectType = "E"; item.Name = "E";
item.PredecessorRowNumber = "0"; rowstringList.Add(item);
item = new TableDataRowStringItem();
item.RowNumber = 2; item.ObjectType = "Function"; item.Name = "Function";
item.PredecessorRowNumber = "1"; rowstringList.Add(item);
item = new TableDataRowStringItem();
item.RowNumber = 3; item.ObjectType = "E"; item.Name = "E";
item.PredecessorRowNumber = "2"; rowstringList.Add(item);
for (int i = 0; i < rowstringList.Count; i++)
{
tableobject.Add(rowstringList[i]);
}
DataGrid2.ItemsSource = tableobject;
}
Your button's visibility is bound to your Hinterlegung variable which has a default value of false. So as best as I can tell, you change it to true in this method - Button_StartTableModus. But, when you reinitialize, the value reverts to false, so you need to set it to true.
Related
I created a editable ComboBox for searching(filtering) like Google. I am using the ActionHandler "KeyUp" and the first input is highlighted and overwritten. How can i disable the overwriting or highlighting?
private void CbInKuLi_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
{
CollectionView itemsViewOriginal = (CollectionView)CollectionViewSource.GetDefaultView(cbInKuLi.ItemsSource);
itemsViewOriginal.Filter = ((o) =>
{
if (String.IsNullOrEmpty(cbInKuLi.Text)) return true;
else
{
DeKreditor x = (DeKreditor)o;
string filterText = cbInKuLi.Text;
if (x.Nummer.ToLowerInvariant().Contains(filterText)
|| (!string.IsNullOrWhiteSpace(x.Firma) && x.Firma.ToLowerInvariant().Contains(filterText))
|| (!string.IsNullOrWhiteSpace(x.Vorname) && x.Vorname.ToLowerInvariant().Contains(filterText))
|| (!string.IsNullOrWhiteSpace(x.Name) && x.Name.ToLowerInvariant().Contains(filterText)))
return true;
else
return false;
}
});
itemsViewOriginal.Refresh();
cbInKuLi.IsDropDownOpen = true;
}
XAML:
<ComboBox
x:Name="cbInKuLi"
StaysOpenOnEdit="True"
IsEditable="True"
IsTextSearchEnabled="False"
HorizontalAlignment="Stretch"
VerticalAlignment="Center"
Grid.Row="0"
Grid.Column="1"
Margin="5,0,5,5"
SelectionChanged="CbInKuLi_SelectionChanged"
KeyUp="CbInKuLi_KeyUp"
TextOptions.TextFormattingMode="Ideal" />
The Text Highlight caused because of setting IsDropDownOpen to true.
The Editable ComboBox auto select the Text if it is Opened, so you could get the TextBox from the template of the ComboBox by its Name than set the selection length to zero at the end of the text.
private void CbInKuLi_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
{
CollectionView itemsViewOriginal = (CollectionView)CollectionViewSource.GetDefaultView(cbInKuLi.ItemsSource);
itemsViewOriginal.Filter = ((o) =>
{
if (String.IsNullOrEmpty(cbInKuLi.Text)) return true;
else
{
Model x = (Model)o;
string filterText = cbInKuLi.Text;
if (x.Text.ToLowerInvariant().Contains(filterText))
return true;
else
return false;
}
});
itemsViewOriginal.Refresh();
cbInKuLi.IsDropDownOpen = true;
var textbox = (TextBox)cbInKuLi.Template.FindName("PART_EditableTextBox", cbInKuLi);
textbox.Select(textbox.Text.Length, textbox.Text.Length);
}
UPDATE:
From comments you can replace the last line by the following line and it is better than the original one:
textbox.CaretIndex = textbox.Text.Length;
Code in context
private void button2_Click(object sender, RoutedEventArgs e)
{
edit();
}
public void edit()
{
textBox1.IsEnabled = true;
textBox2.IsEnabled = true;
textBox3.IsEnabled = true;
textBox4.IsEnabled = true;
textBox5.IsEnabled = true;
textBox6.IsEnabled = true;
textBox7.IsEnabled = true;
textBox8.IsEnabled = true;
textBox9.IsEnabled = true;
textBox10.IsEnabled = true;
textBox11.IsEnabled = true;
textBox12.IsEnabled = true;
textBox13.IsEnabled = true;
textBox14.IsEnabled = true;
textBox15.IsEnabled = true;
textBox16.IsEnabled = true;
textBox17.IsEnabled = true;
textBox18.IsEnabled = true;
}
I want perform the above using a simple for loop that loops through 1-18.
I have tried the followng method but doesn't work as intended
for(i=0;i<19;i++)
{
textBox"" + i + "".IsVisible = true;
}
I'm new to wpf and i'm migrating my app from winforms to wpf.
Use binding.
XAML (MyUserControl):
<UserControl Name="MyControl" ...
....
<TextBox Name="textBox1" IsEnabled="{Binding ElementName=MyControl, Path=AreTextBoxesEnabled}" ... />
<TextBox Name="textBox2" IsEnabled="{Binding ElementName=MyControl, Path=AreTextBoxesEnabled}" ... />
<TextBox Name="textBox3" IsEnabled="{Binding ElementName=MyControl, Path=AreTextBoxesEnabled}" ... />
...
Code-behind (MyUserControl):
public static readonly DependencyProperty AreTextBoxesEnabledProperty = DependencyProperty.Register(
"AreTextBoxesEnabled",
typeof(bool),
typeof(MyUserControl));
public bool AreTextBoxesEnabled
{
get { return (bool)GetValue(AreTextBoxesEnabledProperty); }
set { SetValue(AreTextBoxesEnabledProperty, value); }
}
Just calling AreTextBoxesEnabled = true; will make all the textboxes enabled.
Of course, there are many many other ways. But this is the basic way (without MVVM) of doing it, by harnessing the power of binding.
Simple solution (but not recommended) way is as simple as:
for (i = 0; i < 19; i++)
{
var tb = this.FindName("textBox" + i.ToString()) as TextBox;
if (tb != null) tb.IsEnabled = true;
}
Create a list of text boxes like:
var textBoxes = new List<TextBox>();
// Btw, I don't have a compiler by hand, I assume the type is TextBox.
Fill textBoxes:
textBoxes.Add(textBox1);
textBoxes.Add(textBox2);
...
textBoxes.Add(textBox18);
This is a one-time manual action to fill it. Afterwards you can loop through them:
foreach (var textBox in textBoxes)
{
textBox.IsVisible = true;
}
Or use any other setting/algorithm on the text boxes with the foreach loop (or for, linq etc).
here i m using a wpf ComboBox control and binding it using the datasource.
but my combobox is unable to set default seletion of first index which i give
manualy during the time to binding. here my code shown below can any one tell me how to set default item in combox box.
//Xaml
<ComboBox Height="23" HorizontalAlignment="Left" Margin="142,11,0,0" Name="cmbProductType" VerticalAlignment="Top" Width="180" ItemsSource="{Binding}" />
//Code
private void Window_Loaded(object sender, RoutedEventArgs e)
{
ClsControl.GetProductTypeList(cmbProductType);
}
public static void GetProductTypeList(ComboBox ddlProductType)//Add By Sandeep On 11-03-2013
{
try
{
DataTable dtProductType = null;
try
{
ClsDataLayer objDataLayer = new ClsDataLayer();
dtProductType = objDataLayer.ExecuteDataTable("COMNODE_PROC_GetProductTypeList");
if (dtProductType != null && dtProductType.Rows.Count > 0)
{
DataRow drCardType = dtProductType.NewRow();
drCardType[0] = -1;
drCardType[1] = "< -- Select Card Type -- >";
ddlProductType.SelectedValue = -1;
dtProductType.Rows.InsertAt(drCardType, 0);
ddlProductType.ItemsSource = dtProductType.DefaultView;
ddlProductType.DisplayMemberPath = "PRODUCT_TYPE";
ddlProductType.SelectedValuePath = "PRODUCT_TYPE_ID";
}
}
catch (Exception)
{
throw;
}
}
catch
{
}
}
Try updating your XAML to this:
<ComboBox Height="23"
HorizontalAlignment="Left"
Margin="142,11,0,0"
Name="cmbProductType"
VerticalAlignment="Top"
Width="180"
ItemsSource="{Binding}"
IsSynchronizedWithCurrentItem="True" />
Also, use SelectedIndex instead when you select the item:
ddlProductType.SelectedIndex = 0;
dtProductType.Rows.InsertAt(drCardType, 0);
ddlProductType.ItemsSource = dtProductType.DefaultView;
ddlProductType.DisplayMemberPath = "PRODUCT_TYPE";
ddlProductType.SelectedValuePath = "PRODUCT_TYPE_ID"
I have defined this in XAML:
<phone:PhoneApplicationPage.Resources>
<DataTemplate x:Key="sendContentTemplate">
<StackPanel>
<TextBlock Name="P1Label" Text="1" Visibility="Collapsed" />
<TextBox Name="P1" Visibility="Collapsed" />
<TextBlock Name="P2Label" Text="2" Visibility="Collapsed" />
<TextBox Name="P2" Visibility="Collapsed" />
<TextBlock Name="P3Label" Text="3" Visibility="Collapsed" />
<TextBox Name="P3" Visibility="Collapsed" />
</StackPanel>
</DataTemplate>
</phone:PhoneApplicationPage.Resources>
Then, in code, I define a CustomMessageBox from Windows Phone Toolkit in this way:
DataTemplate contentTemplate = (DataTemplate)Resources["sendContentTemplate"];
SetVisibility(Contrato[0], contentTemplate);
var messageBox = new CustomMessageBox
{
Caption = "Send",
Message = "",
ContentTemplate = contentTemplate,
LeftButtonContent = "Send",
RightButtonContent = "Close"
};
messageBox.Show();
I'm trying to modify properties in the DataTemplate in code, but without success. Now, I'm doing this:
private void SetVisibility(Contrato contrato, DataTemplate dataTemplate)
{
var controls = AllChildren(dataTemplate.LoadContent());
if (contrato.Number == 1)
{
controls.Find(c => c.Name == "P1Label").Visibility = Visibility.Visible;
controls.Find(c => c.Name == "P1").Visibility = Visibility.Visible;
}
}
private List<FrameworkElement> AllChildren(DependencyObject parent)
{
var list = new List<FrameworkElement>();
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++)
{
var child = VisualTreeHelper.GetChild(parent, i);
if (child is FrameworkElement)
{
list.Add((FrameworkElement)child);
}
list.AddRange(AllChildren(child));
}
return list;
}
Is it possible to modify a DataTemplate property by code?
Finally, I did it by adding controls by code. I couldn't find another way to do it.
I did it in this way:
var messageBox = new CustomMessageBox {
Caption = "Send",
Message = "",
Content = CreateMessageBoxContent(c.T),
LeftButtonContent = "Send",
RightButtonContent = "Close"
};
private static object CreateMessageBoxContent(string t)
{
var stackPanel = new StackPanel();
var P1Label = new TextBlock {Text = AppResources.P1Label};
var P2Label = new TextBlock {Text = AppResources.P2Label};
var P3Label = new TextBlock {Text = AppResources.P3Label};
var P1 = new TextBox();
var P2 = new TextBox();
var P3 = new TextBox();
if (t == "T2" || t == "T20")
{
stackPanel.Children.Add(P1Label);
stackPanel.Children.Add(P1);
}
else if (t =="T20D" || t == "T21D")
{
stackPanel.Children.Add(P1Label);
stackPanel.Children.Add(P1);
stackPanel.Children.Add(P3Label);
stackPanel.Children.Add(P3);
}
else if (t == "T3" || t == "T31")
{
stackPanel.Children.Add(P1Label);
stackPanel.Children.Add(P1);
stackPanel.Children.Add(P2Label);
stackPanel.Children.Add(P2);
stackPanel.Children.Add(P3Label);
stackPanel.Children.Add(P3);
}
return stackPanel;
}
I am working on a WPF application in C# (.NET 4.0) where I have a ListView with a GridView that has two columns.
I dynamically want to add rows (in code). My dilemma is that only the first column will have regular text added to it. The second column will have an object that includes a multi column Grid with TextBlocks. (see link http://imageshack.us/photo/my-images/803/listview.png/)
If I do what you would normally do when you want to enter text in all columns (ie. DisplayMemberBinding) all I get in the second column is the text "System.Windows.Grid", which obviously isn't what I want.
For reference if I just try to add the Grid object (with the TextBlocks) with the code listView1.Items.Add(grid1) (not using DisplayMemberBinding) the object gets added to the second column only (with the first column being blank) and not how it normally works with text where the same text ends up in all columns.
I hope my question is detailed enough and any help with this would be much appreciated.
EDIT:
I have tried the following code, howeever every time I click the button to add a new row every single row gets updated with the same datatemplate. (ie. the second column always shows the same data on every row.)
xaml:
<Window x:Class="TEST.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Name="AAA" Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded">
<Grid Name="grid1">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="374*" />
<ColumnDefinition Width="129*" />
</Grid.ColumnDefinitions>
<Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="21,12,0,0" Name="button1" VerticalAlignment="Top" Width="75" Grid.Column="1" Click="button1_Click" />
</Grid>
code:
public partial class MainWindow : Window
{
ListView listView1 = new ListView();
GridViewColumn viewCol2 = new GridViewColumn();
public MainWindow()
{
InitializeComponent();
Style style = new Style(typeof(ListViewItem));
style.Setters.Add(new Setter(ListViewItem.HorizontalContentAlignmentProperty,
HorizontalAlignment.Stretch));
listView1.ItemContainerStyle = style;
GridView gridView1 = new GridView();
listView1.View = gridView1;
GridViewColumn viewCol1 = new GridViewColumn();
viewCol1.Header = "Option";
gridView1.Columns.Add(viewCol1);
viewCol2.Header = "Value";
gridView1.Columns.Add(viewCol2);
grid1.Children.Add(listView1);
viewCol1.DisplayMemberBinding = new Binding("Option");
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
}
private void button1_Click(object sender, RoutedEventArgs e)
{
DataTemplate dataTemplate = new DataTemplate();
FrameworkElementFactory spFactory = new FrameworkElementFactory(typeof(Grid));
Random random = new Random();
int cols = random.Next(1, 6);
int full = 100;
for (int i = 0; i < cols; i++)
{
FrameworkElementFactory col1 = new FrameworkElementFactory(typeof(ColumnDefinition));
int partWidth = random.Next(0, full);
full -= partWidth;
col1.SetValue(ColumnDefinition.WidthProperty, new GridLength(partWidth, GridUnitType.Star));
spFactory.AppendChild(col1);
}
if (full > 0)
{
FrameworkElementFactory col1 = new FrameworkElementFactory(typeof(ColumnDefinition));
col1.SetValue(ColumnDefinition.WidthProperty, new GridLength(full, GridUnitType.Star));
spFactory.AppendChild(col1);
}
for (int i = 0; i < cols; i++)
{
FrameworkElementFactory text1 = new FrameworkElementFactory(typeof(TextBlock));
SolidColorBrush sb1 = new SolidColorBrush();
switch (i)
{
case 0:
sb1.Color = Colors.Blue;
break;
case 1:
sb1.Color = Colors.Red;
break;
case 2:
sb1.Color = Colors.Yellow;
break;
case 3:
sb1.Color = Colors.Green;
break;
case 4:
sb1.Color = Colors.Purple;
break;
case 5:
sb1.Color = Colors.Pink;
break;
case 6:
sb1.Color = Colors.Brown;
break;
}
text1.SetValue(TextBlock.BackgroundProperty, sb1);
text1.SetValue(Grid.ColumnProperty, i);
spFactory.AppendChild(text1);
}
if (full > 0)
{
FrameworkElementFactory text1 = new FrameworkElementFactory(typeof(TextBlock));
SolidColorBrush sb1 = new SolidColorBrush(Colors.Black);
text1.SetValue(TextBlock.BackgroundProperty, sb1);
text1.SetValue(Grid.ColumnProperty, cols);
spFactory.AppendChild(text1);
}
dataTemplate.VisualTree = spFactory;
viewCol2.CellTemplate = dataTemplate;
int rows = listView1.Items.Count + 1;
listView1.Items.Add(new { Option = "Row " + rows });
}
}
You do not have to use the DisplayMemberBinding, for complex content you can use the CellTemplate, e.g.:
<ListView ItemsSource="{Binding Data}">
<ListView.View>
<GridView>
<GridViewColumn DisplayMemberBinding="{Binding Name}" />
<GridViewColumn>
<GridViewColumn.CellTemplate>
<DataTemplate>
<!-- This should display your grid if it is a property on your item called GridProperty -->
<ContentControl Content="{Binding GridProperty}" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>