如何将每个GridViewColumn绑定到单独的ObservableCollection?

时间:2011-03-03 00:54:30

标签: c# .net wpf data-binding listview

首先,我将此用于ListView控件本身:

ItemsSource="{Binding AllEffects}"

那么这不允许每GridViewColumn个人绑定吗?

除了AllEffects我还有另外两个ObservableCollection我要绑定到另外两个GridViewColumn

public ObservableCollection<GPUSupportNode> GPUSupport { get; set; }
public ObservableCollection<CPUSupportNode> CPUSupport { get; set; }

public class CPUSupportNode
{
    public bool IsSupported {get;set;}
}

我正试图将这两个GridViewColumns分别挂钩到GPUSupport/CPUSupport .IsSupported

现在我有:

<GridViewColumn
    Width="Auto"
    Header="GPU">
    <GridViewColumn.CellTemplate>
        <DataTemplate>
            <CheckBox
                Margin="0"
                HorizontalAlignment="Center"
                IsChecked="{Binding GPUSupport, Mode=TwoWay}"/>
        </DataTemplate>
    </GridViewColumn.CellTemplate>
</GridViewColumn>

但它不起作用。这可能吗?

1 个答案:

答案 0 :(得分:3)

你可以尝试像这样创建一个ViewModel类

public class MyViewModel
{

 public ObservableCollection<GPUSupportNode> GPUSupport { get; set; }
 public ObservableCollection<CPUSupportNode> CPUSupport { get; set; }
 public ObservableCollection<TypeOfEffects> AllEffects{ get; set; }

}

并创建一个MyViewModel实例,比如ViewModel并将其初始化为Observable Collections。设置ItemsSource

ItemsSource="{Binding ViewModel}"  

然后这应该有用。

<GridViewColumn
    Width="Auto"
    Header="GPU">
    <GridViewColumn.CellTemplate>
        <DataTemplate>
            <CheckBox
                Margin="0"
                HorizontalAlignment="Center"
                IsChecked="{Binding GPUSupport, Mode=TwoWay}"/>
        </DataTemplate>
    </GridViewColumn.CellTemplate>
</GridViewColumn>