I have a datagrid which has four columns of combobox in wpf. At the starting, the first combobox is enabled. After making a selection on the first combobox, the second combobox gets enabled. I am unable to access the combobox name property in my xaml.cs file so that I can enable the next combobox column after successful selection of the first one. Can you suggest how to access the combobox property which is present inside a datagrid in my xaml.cs file ?
This is my xaml code
<DataGridTemplateColumn
Header ="Example 9">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding PartIds, RelativeSource={RelativeSource AncestorType=Window}}" SelectedItem ="{Binding PartId,UpdateSourceTrigger=PropertyChanged}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
Assuming you have two columns in your DataGrid
, the first column contains a combobox
, and its selecteditem
is bound to a property named PartId
, the second column contains also a combobox
with a selecteditem
bound to a property named PartId2
, your model should look something like that:
public class Model
{
public string PartId { get; set; }
public string PartId2 { get; set; }
}
Now, assuming your DataGrid
's itemsource
is bound to an ObservableCollection
named DgCollection
:
private ObservableCollection<Model> _dgCollection;
public ObservableCollection<Model> DgCollection
{
get { return _dgCollection; }
set
{
if (Equals(value, _dgCollection)) return;
_dgCollection = value;
OnPropertyChanged();
}
}
The second column could use a DataTrigger
to activate its combobox
once the selecteditem
of the first column is set, like so:
<DataGrid ItemsSource="{Binding DgCollection}" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTemplateColumn
Header ="Example 9">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding PartIds, RelativeSource={RelativeSource AncestorType=Window}}" SelectedItem ="{Binding PartId,UpdateSourceTrigger=PropertyChanged}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn
Header ="Example 10">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox x:Name="FirstCbx" ItemsSource="{Binding PartIds, RelativeSource={RelativeSource AncestorType=Window}}" SelectedItem ="{Binding PartId2,UpdateSourceTrigger=PropertyChanged}" >
<ComboBox.Style>
<Style TargetType="ComboBox">
<Setter Property="IsEnabled" Value="True"/>
<Style.Triggers>
<DataTrigger Binding="{Binding PartId}" Value="{x:Null}">
<Setter Property="IsEnabled" Value="False"/>
</DataTrigger>
</Style.Triggers>
</Style>
</ComboBox.Style>
</ComboBox>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
You could generalize this to four columns easily.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.