使用以下模型绑定(bind)WPF组合框列时出现问题:
public class Game
{
public string Name;
ObservableCollection<Player> Players{get; set;}
}
public class Player
{
public int ID;
public string PlayerName;
public List<string> AllSelectionStatus = new List<string> {"Yes", "No", "Waiting"};
public string PlayerSelectionStatus;
}
public class PlayerModel
{
ObservableCollection<Game> Games {get; set;}
}
我需要在WPF DataGrid中显示上述结构,如下所示。
每个组合框的“ItemSource”属性值均由AllSelectionStatus设置,而“SelectedItem”属性值由PlayerSelectionStatus设置。
这是我的XAML代码。
<DataGrid Name="dgRowDetails" Background="Transparent" Foreground="Black" SelectionMode="Single" HeadersVisibility="Column"
ItemsSource="{Binding Games[0].Players}">
<DataGrid.Resources>
<Style x:Key="DataGridComboBoxColumnStyle0" BasedOn="{StaticResource BaseComboBoxBoxStyle}" TargetType="ComboBox">
<Setter Property="ItemsSource" Value="{Binding AllSelectionStatus, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" />
<Setter Property="SelectedItem" Value="{Binding PlayerSelectionStatus, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
<Style.Triggers>
<DataTrigger Binding="{Binding IsVisible}" Value="False">
<Setter Property="Visibility" Value="Collapsed" />
</DataTrigger>
<DataTrigger Binding="{Binding IsSystemEnabled}" Value="False">
<Setter Property="IsEnabled" Value="False" />
</DataTrigger>
</Style.Triggers>
</Style>
<Style x:Key="DataGridComboBoxColumnStyle1" BasedOn="{StaticResource BaseComboBoxBoxStyle}" TargetType="ComboBox">
<Setter Property="ItemsSource" Value="{Binding AllSelectionStatus, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" />
<Setter Property="SelectedItem" Value="{Binding PlayerSelectionStatus, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
<Style.Triggers>
<DataTrigger Binding="{Binding IsVisible}" Value="False">
<Setter Property="Visibility" Value="Collapsed" />
</DataTrigger>
<DataTrigger Binding="{Binding IsSystemEnabled}" Value="False">
<Setter Property="IsEnabled" Value="False" />
</DataTrigger>
</Style.Triggers>
<!-- The 3rd Combobox style not shown as it is similar to the one above-->
</Style>
此XAML的问题在于,Game2和Game3组合框没有自己的PlayerSelectionStatus。 Game2和Game3都获得Game1 PlayerSelectionStatus。
需要帮助解决此问题。感谢你的帮助。
最佳答案
只需在IsSynchronizedWithCurrentItem
样式内将false
设置为ComboBox
即可。
<Setter Property="IsSynchronizedWithCurrentItem" Value="False"/>
以您的示例为例:
<DataGrid Name="dgRowDetails" Background="Transparent" Foreground="Black" SelectionMode="Single" HeadersVisibility="Column"
ItemsSource="{Binding Games[0].Players}">
<DataGrid.Resources>
<Style x:Key="DataGridComboBoxColumnStyle0" BasedOn="{StaticResource BaseComboBoxBoxStyle}" TargetType="ComboBox">
<Setter Property="ItemsSource" Value="{Binding AllSelectionStatus, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" />
<Setter Property="SelectedItem" Value="{Binding PlayerSelectionStatus, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
<Setter Property="IsSynchronizedWithCurrentItem" Value="False"/>
<Style.Triggers>
<DataTrigger Binding="{Binding IsVisible}" Value="False">
<Setter Property="Visibility" Value="Collapsed" />
</DataTrigger>
<DataTrigger Binding="{Binding IsSystemEnabled}" Value="False">
<Setter Property="IsEnabled" Value="False" />
</DataTrigger>
</Style.Triggers>
</Style>
<Style x:Key="DataGridComboBoxColumnStyle1" BasedOn="{StaticResource BaseComboBoxBoxStyle}" TargetType="ComboBox">
<Setter Property="ItemsSource" Value="{Binding AllSelectionStatus, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" />
<Setter Property="SelectedItem" Value="{Binding PlayerSelectionStatus, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
<Setter Property="IsSynchronizedWithCurrentItem" Value="False"/>
<Style.Triggers>
<DataTrigger Binding="{Binding IsVisible}" Value="False">
<Setter Property="Visibility" Value="Collapsed" />
</DataTrigger>
<DataTrigger Binding="{Binding IsSystemEnabled}" Value="False">
<Setter Property="IsEnabled" Value="False" />
</DataTrigger>
</Style.Triggers>
<!-- The 3rd Combobox style not shown as it is similar to the one above-->
</Style>
关于c# - 使用ObservableCollection绑定(bind)WPF DataGrid组合框列的问题,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51794956/