In the previous window I have a table. I can access specific data entries.
for example the code below takes the Description from the selected row, and places it inside of the textbox.
<TextBlock Text="Description" Style="{StaticResource tabTextBlock}"/>
<ComboBox ItemsSource="{Binding Vwr.Table.Tbl, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
DisplayMemberPath="Description" SelectedValuePath="Description"
SelectedIndex="{Binding Vwr.Table.SelectedRowIndex, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
Text="{Binding Vwr.Table.Vals[2].Val, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
IsEditable="True" Style="{StaticResource tabTextBox}" Height="54"/>
My question is:
How would I port the entire Table, so that it would be displayed in a DataGrid/Table format inside of an XAML Window?
Assuming your data context is similar to the example you gave, this should work:
<DataGrid ItemsSource="{Binding Vwr.Table.Tbl}" />
By default, the DataGrid will automatically generate columns for each property in the item type.
Related
I have an issue with binding in ComboBox. I have searched in Google and so, but I haven't been able to find the answer.
I have silverlight form with combobox like this:
<ComboBox x:Name="FirmBox"
Grid.Row="23"
Grid.Column="1"
Grid.ColumnSpan="2"
Margin="5,5,5,0"
SelectedValuePath="{Binding Path=Value, Mode=TwoWay}"
SelectedItem="{Binding Path=Firm, Mode=TwoWay}">
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Path=Value}"/>
<TextBlock Text="{Binding Path=Key}"/>
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
And the ItemsSource is an
ObservableCollection<KeyValue<String, KeyValue<String, String>>>
So I've figured out how to display this in right way, but I don't know how to bind the selected item to my KeyValuePair<String, String>
field. That does not seem obvious to me. So I need to bind the value of selected item to my field and don't know how to do it.
Thank you.
The solution was simple as always:
SelectedValuePath="Value"
SelectedValue="{Binding Path=Firm, Mode=TwoWay}">
I am using the following code to use a ComboBox inside my DataGridTemplateColumn:
<DataGridTemplateColumn Header="MyHeader" Width="50">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Width="Auto" Text="{Binding Path=MyVal}" ToolTip="{Binding MyDisplayName}"></TextBlock>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<ComboBox Width="50" Height="17" ItemsSource="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type UserControl}},Path=DataContext.MyList}"
SelectedValuePath="MyValPath" SelectedValue="{Binding MySelectedVal}" SelectedItem="{Binding MyObject}" DisplayMemberPath="MyDisplayName"
FontSize="12" >
</ComboBox>
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>
CellTemplate is for showing my text (custom text for selected value) and CellEditingTemplate contains my ComboBox which has the actual list.
When I select a value from my drop-down, I have to click on another part of the data grid to get DataGridDiagnosticCellEditEnding fired.
I want to get it fired as soon as I select a value or change a value from my ComboBox.
I tried adding the following code, but it didn't work:
<ComboBox Width="50" Height="17" ItemsSource="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type UserControl}},Path=DataContext.MyList}"
SelectedValuePath="MyValPath" SelectedValue="{Binding MySelectedVal}" SelectedItem="{Binding MyObject, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" DisplayMemberPath="MyDisplayName" FontSize="12" >
Also I tried adding IsSynchronizedWithCurrentItem="True".
I gave a try to your code, and ve got a fix :
On the ComboBox of the DataGrid Column, subscribe to the closing event
Implement the event Handler and raise a routed Event.
CommitEditCommand belongs to the DataGrid class :
private void ComboBoxDropDownClosed(object sender, EventArgs e)
{
DataGrid.CommitEditCommand.Execute(datagrid1,datagrid1);
}
From there you fall in the DataGrid.CellEditting breakpoint
Here is the working solution : http://1drv.ms/1LFB5Gc
Regards
Recently I asked similar questions here and here, but could not fix the issue properly.
I have a DataGrid with ComboBox that can contain either a selected item or not. But if it does then the ComboBox should select it when the dropdown is opening which is doesn't.
Currently I have this code which works except when opening the dropdown the first time. It is nothing selected.
<DataGridTemplateColumn Header="Company">
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}, Path=DataContext.Companies}"
SelectedItem="{Binding Company, Converter={StaticResource NullValueConverter}}"/>
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Company, Converter={StaticResource NullValueConverter}}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
The NullValueConverter prevents exceptions if the Company is null.
If your company was not a string as you mentioned in the comments then it can't display the Name unless you set
DisplayMemberPath="Name" or create an ItemTemplate.
You should use SelectedItem="{Binding
Company,UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}" to update
the value as soon as it changes because if you don't it will only
raise ProperyChanged when you focus another cell or row.
if your Property was immutable or a value type then you
should use SelectedValue="{Binding Path=Company,UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}" SelectedValuePath="Content"
.
I have a Combobox in a DataGrid which allows IsTextSearchEnabled. This works but the user is allowed to put their own text when the item is not found in the combobox. Is there a property that will stop this, or what can I do to stop the user adding their own text?
The xaml
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding DataContext.Types,
RelativeSource={RelativeSource Mode=FindAncestor,
AncestorType=Window}}"
x:Name="cmbDeploymentEditType"
SelectedItem="{Binding Name, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
SelectedValue="Type"
Text="{Binding Name, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
IsTextSearchEnabled="True"
IsSynchronizedWithCurrentItem="False"
IsEditable="True">
</ComboBox>
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
Thanks
There are multiple ways you can handle this, you can use a control that stops this like the XamMultiColumnComboEditor with CustomValueEnteredAction setting. Or you could do validation to enforce the rule you want.
I want to make my ComboBox editable. My Combobox displays a list of countries, and I would like my users to be able to type in the country to search for it. However, in its current state, it doesn't do that. What have I done wrong?
<ComboBox x:Name="CmbCountryList" Width="150"
IsEditable="True" IsTextSearchEnabled="True"
IsTextSearchCaseSensitive="False" StaysOpenOnEdit="True"
TextSearch.TextPath="CountryName"
ItemsSource="{Binding CountryMasterList, Mode=TwoWay}"
DisplayMemberPath="CountryName"
SelectedValuePath="CountryID"
SelectedItem="{Binding Path=CountryObj, Mode=TwoWay, ValidatesOnDataErrors=True}"
Text="{Binding Path=CountryName, Mode=TwoWay}"
IsSynchronizedWithCurrentItem="False" />
Check http://weblogs.asp.net/okloeten/archive/2007/11/12/5088649.aspx for an example of how you can implement this.