使用ObservableCollection绑定WPF DataGrid组合框列的问题

时间:2018-08-10 22:59:55

标签: c# wpf xaml mvvm

使用以下模型绑定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中显示上述结构,如下所示。

enter image description here

每个组合框的“ 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。

需要帮助来解决此问题。感谢您的帮助。

2 个答案:

答案 0 :(得分:0)

只需在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>    

答案 1 :(得分:0)

您的数据网格的ItemSource只是第一个游戏

    ItemsSource="{Binding Games[0].Players}

,每个游戏对象只有一个PlayerSelectionStatus,它绑定到所有三个组合框。游戏[1]或游戏[2]或完整的PlayerModel游戏列表均不受约束。

我认为您需要重新考虑数据结构,以便每个玩家都有一个游戏列表(或三个Game属性),每个游戏都有其自己的状态。