2

列挙型を使用して、XAML の ObservableCollection からアイテムにアクセスできるようにしたいと考えています。

次の方法で、ObservableCollection にバインドし、XAML でどのアイテムを指定するかを指定できます。

<Window x:Class="ArrayPropertyBinding.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:ArrayPropertyBinding"
        Title="MainWindow" Height="350" Width="525">
    <Window.DataContext>
        <local:MyViewModel />
    </Window.DataContext>
    <StackPanel>
        <CheckBox Content="First" IsChecked="{Binding FilterBy[0],Mode=TwoWay}"/>
        <CheckBox Content="Second" IsChecked="{Binding FilterBy[1],Mode=TwoWay}"/>
        <CheckBox Content="Thrid" IsChecked="{Binding FilterBy[2],Mode=TwoWay}"/>
        <CheckBox Content="Fourth" IsChecked="{Binding FilterBy[3],Mode=TwoWay}"/>
        <Button Content="Print" Click="Button_Click_1"/>
        <Button Content="SetFourthTrue" Click="Button_Click_SetFourthTrue"/>
    </StackPanel>
</Window>

ビューモデルは次のようになりますが

public class MyViewModel
{
    public enum Filters
    {
        First = 0,
        Second,
        Thrid,
        Fourth
    }

    private ObservableCollection<bool> _filterBy = new ObservableCollection<bool>() { false, false, false, false };
    public ObservableCollection<bool> FilterBy
    {
        get { return _filterBy; }
    }

    public void PrintFilters()
    {
        System.Diagnostics.Debug.Write("<<<<");
        foreach (bool b in _filterBy)
        {
            System.Diagnostics.Debug.Write(b);
            System.Diagnostics.Debug.Write(" ");
        }
        System.Diagnostics.Debug.WriteLine(">>>>");
    }

    public void SetFourthTrue()
    {
        FilterBy[(int)Filters.Fourth] = true;
    }
}

XAML を次のように記述できるようにしたいと考えています。

<CheckBox Content="First" IsChecked="{Binding FilterBy[Filters.First],Mode=TwoWay}"/>

ポインタやアイデアは大歓迎です。

4

2 に答える 2

3

値を直接入力しようとすることは不可能であるように見えるため、おそらくカスタムバインディングパスの構築に要約されます。enum

構文は次のようになります。

{Binding Path={me:PathConstructor FilterBy[(0)], {x:Static myns:Filters.First}}}

(列挙型をクラスから移動することをお勧めします。そうでないmyns:MyViewModel+Filters.First場合は、私が間違っていなければです)

于 2013-07-22T18:24:15.057 に答える