如何结合使用多重复选框-wpf

时间:2018-11-12 08:30:46

标签: c# wpf checkbox binding datacontext

我想为每个复选框分配操作,但是我不知道怎么做,因为我使用绑定。

下面是XAML.cs

public MainWindow()
    {
        InitializeComponent();

        Collection1 = new string[] { "Female", "Male", "Animal", "Safe", "Soft", "Hard", "Space", "Landscape", "Outside", "Inside", "City", "France", "Flower", "Sunset", "Sky", "Fireworks", "Spring", "Winter", "Summer", "Fall", "Christmas", "Halloween", "Ghost", "Demon", "Angel", "Watermelon", "Storm", "Waterfall", "Night", "Sun","Moon", "Food", "Cheese", "Kancolle", "IT", "UFO", "Travel", "Sport", "Nightmare"};

        Collection2 = new string[] { "website1", "website2", "website3", "website4", "website5","website6", "website7", "website8", "website9", "website10", "website11", "website12", "website13", "website14", "website15", "website16", "website17", "website18" };

        DataContext = this;
    }

    private void CB_catego_Checked(object sender, RoutedEventArgs e)
    {
        MessageBox.Show("this is an categorie");
    }

    private void CB_website_Checked(object sender, RoutedEventArgs e)
    {
        MessageBox.Show("this is an website");
    }

这是我的XAML:

<ScrollViewer HorizontalAlignment="Left" Height="100" VerticalAlignment="Top" Width="97" Margin="10,209,0,0">
        <ListBox ItemsSource="{Binding Collection1}">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <CheckBox Checked="CB_catego_Checked" Content="{Binding .}" />
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </ScrollViewer>
    <ScrollViewer HorizontalAlignment="Left" Height="107" VerticalAlignment="Top" Width="97" Margin="10,97,0,0">
        <ListBox ItemsSource="{Binding Collection2}">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <CheckBox Checked="CB_website_Checked" Content="{Binding .}" />
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </ScrollViewer>

你知道是否有可能? 非常感谢。

1 个答案:

答案 0 :(得分:0)

我终于找到了解决方法。

 private void CB_website_Checked(object sender, RoutedEventArgs e)
    {
        boards.Add(strBoard[((CheckBox)(sender)).Content.ToString()]);
    }

由于我的字典,这一行允许我找到选中了哪个复选框:

        private readonly Dictionary<string, Board> strBoard = new Dictionary<string, Board>()
    {
        { "website1", Board.website1 },
        { "website2", Board.website2 },
        { "website3", Board.website3 },
        { "website4", Board.website4 },
        { "website5", Board.website5 },
        { "website6", Board.website6 },
        { "website7", Board.website7 },
        { "website8", Board.website8 },
        { "website9", Board.website9 },
        { "website10", Board.website10 },
        { "website11", Board.website11 },
        { "website12", Board.website12 },
        { "website13", Board.website13 },
        { "website14", Board.website14 },
        { "website15", Board.website15 },
        { "website16", Board.website16 },
        { "website17", Board.website17 },
        { "website18", Board.website18 }
    };