简体   繁体   English

如何双向绑定字典 <enum, bool> 到WPF中的ListView列?

[英]How to 2-way bind a Dictionary<enum, bool> to a ListView column in WPF?

I have a type like this: 我有这样的类型:

public class EffectViewModel
{
    public string Name { get; set; }
    public string Category { get; set; }

    public Dictionary<ShaderType, bool> ShaderSupport { get; set; }
}

.Name and .Category is already binded to 2 separate columns, but the ShaderSupport dictionary isn't. .Name.Category已绑定到2个单独的列,但ShaderSupport字典不是。

I can't figure out how to 2-way bind the dictionary to a separate column for each ShaderType . 我无法弄清楚如何将字典双向绑定到每个ShaderType的单独列。 I don't know if these number of columns can be done dynamically but I hardcoded them in the xaml, so like: 我不知道这些列数是否可以动态完成,但我在xaml中对它们进行了硬编码,如下所示:

<GridViewColumn Width="60" Header="GPU" >
<GridViewColumn Width="60" Header="Pixel" >
...

But now stuck on the binding part. 但现在卡在绑定部分。 Any ideas? 有任何想法吗?

<ItemsControl Grid.IsSharedSizeScope="True" ItemsSource="{Binding AllEffects}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal">
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto" SharedSizeGroup="NameColumn" />
                        <ColumnDefinition Width="Auto" SharedSizeGroup="CategoryColumn" />
                    </Grid.ColumnDefinitions>
                    <TextBlock Text="{Binding Name}" />
                    <TextBlock Text="{Binding Category}" Grid.Column="1" />
                </Grid>
                <ItemsControl ItemsSource="{Binding ShaderSupport}">
                    <ItemsControl.ItemsPanel>
                        <ItemsPanelTemplate>
                            <StackPanel Orientation="Horizontal" />
                        </ItemsPanelTemplate>
                    </ItemsControl.ItemsPanel>
                    <ItemsControl.ItemTemplate>
                        <DataTemplate>
                            <Grid>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="60" />
                                </Grid.ColumnDefinitions>
                                <CheckBox Grid.Row="1" IsChecked="{Binding Value, Mode=OneWay}" />
                            </Grid>
                        </DataTemplate>
                    </ItemsControl.ItemTemplate>
                </ItemsControl>
            </StackPanel>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

This will create a grid without a header. 这将创建一个没有标题的网格。 In order to make checkbox bound in TwoWay mode replace dictionary with your own type, KeyValuePair doesn't allow to write Value property and hence cannot be used for TwoWay binding. 为了在TwoWay模式下使用自己的类型替换字典, KeyValuePair不允许写入Value属性,因此不能用于TwoWay绑定。

Please note that each EffectVM should have all Shaders in the dictionary in the same order. 请注意,每个EffectVM应该以相同的顺序在字典中包含所有着色器。

Since dictionary is an IEnumerable you can bind the key and value. 由于字典是IEnumerable,您可以绑定键和值。 That said order is not always consistent with dictionaries so I would have my view model create a wrapper observablecollection 那说顺序并不总是与字典一致,所以我会让我的视图模型创建一个包装器observablecollection

<ListView ItemsSource="{Binding ShaderSupport}">
    <ListView.View>
        <GridView>
            <GridView.Columns>
                <GridViewColumn Width="60" Header="Key" DisplayMemberBinding="{Binding Key}" />
                <GridViewColumn Width="60" Header="Value" DisplayMemberBinding="{Binding Value}" />
            </GridView.Columns>
        </GridView>
    </ListView.View>
</ListView>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM