【问题标题】:UWP is binded checkbox is checked?UWP 已绑定复选框是否已选中?
【发布时间】:2018-10-25 12:50:48
【问题描述】:

所以我创建了一个包含几个绑定复选框的组合框。我的 xml 看起来像这样:

<ComboBox x:Name="CbSandwichFilling" ItemsSource="{x:Bind SandwichFillingList}" PlaceholderText="Choose sandwich filling">
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <CheckBox Name="{Binding Ingredient_name}" Content="{Binding Ingredient_name}"/>
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>

我的 C# 看起来像这样:

private List<Ingredients> sandwichFilling;

public List<Ingredients> SandwichFillingList
{
    get { return sandwichFilling; }
    set { sandwichFilling = value; }
}

public BasicSandwiches()
{
    sandwichFilling = Ingredients.GetIngredients("sandwichFilling");
    this.DataContext = SandwichFillingList;
}

GetIngredients("sandwichFilling") 函数从数据库中接收三明治馅料并将它们放入 ComboBox 内的 Checkbox 中。

当用户按下按钮时,我希望程序知道哪些复选框被选中。我该怎么做?

【问题讨论】:

  • WPF、UWP 和 XAML 在设计时考虑了 MVVM 模式。虽然您可以使用其他方法,但这样做会失去大约 90% 的功能,并且在每个角落都会遇到问题。这看起来根本不像 MVVM。在 MVVM 模式中,inrgredients 将公开 Checkbox 也会绑定的“选定”属性,从而使答案变得微不足道(只需查看 bool 值)。几年前我写了一篇关于 MVVM 的简短介绍,但它仍然可以帮助你继续前进:social.msdn.microsoft.com/Forums/vstudio/en-US/…
  • 理想情况下,您的成分类应该有一个 IsSelected 属性,您可以将其与 CheckBox 的 IsChecked 属性绑定。现在,当您单击按钮时,您只需遍历 SandwichFillingList 集合并检查哪些成分的 IsSelected 属性设置为 true。

标签: c# xaml uwp uwp-xaml


【解决方案1】:

当用户按下按钮时,我希望程序知道哪些复选框被选中。我该怎么做?

根据您的要求,您需要创建数据源进行绑定。下面是继承INotifyPropertyChanged接口的数据模型。

public class Ingredients : INotifyPropertyChanged
{
    public Ingredients()
    {

    }
    public event PropertyChangedEventHandler PropertyChanged;
    private void OnPropertyChang([CallerMemberName] string propertyName = null)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
    private string _Ingredient_name;
    public string Ingredient_name
    {
        get
        {
            return _Ingredient_name;
        }
        set
        {
            _Ingredient_name = value;
            OnPropertyChang();
        }
    }

    private bool _IsCheck;
    public bool IsCheck
    {
        get
        {
            return _IsCheck;
        }
        set
        {
            _IsCheck = value;
            OnPropertyChang();
        }
    }
}

然后创建您的MainPageViewModel,用于与 xaml 代码绑定。

public class MainPageViewModel
{
    public ObservableCollection<Ingredients> Items { get; set; }
    public MainPageViewModel()
    {
        Items = new ObservableCollection<Ingredients>()
        {
            new Ingredients()
            {
                Ingredient_name= "Nico",
                IsCheck=false
            },
               new Ingredients()
            {
                Ingredient_name= "mimi",
                IsCheck=false
            },
                  new Ingredients()
            {
                Ingredient_name= "kiki",
                IsCheck=false
            },
                     new Ingredients()
            {
                Ingredient_name= "zizi",
                IsCheck=false
            },
                        new Ingredients()
            {
                Ingredient_name= "sasa",
                IsCheck=false
            },

        };

    }
}

用法

<Page.DataContext>
    <local:MainPageViewModel x:Name="ViewModel" />
</Page.DataContext>

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="4*"/>
        <RowDefinition Height="1*"/>

        <RowDefinition Height="4*"/>
    </Grid.RowDefinitions>
    <ComboBox  x:Name="CbSandwichFilling" ItemsSource="{Binding Items}" PlaceholderText="Choose sandwich filling">
        <ComboBox.ItemTemplate>
            <DataTemplate>
                <CheckBox Content="{Binding Ingredient_name}" IsChecked="{Binding IsCheck,Mode=TwoWay}" />
            </DataTemplate>
        </ComboBox.ItemTemplate>
    </ComboBox>
    <Button Grid.Row="1" Click="Button_Click" Content="ShowAllItem"/>
    <TextBlock Grid.Row="2" Name="InfoDisplay" />
</Grid>

注意你需要将IsChecked属性的绑定模式设置为TwoWay,这样你检查的时候就可以改变数据源。而且您不能将Name 属性绑定到Ingredient_name,否则它会抛出xaml 异常。

MainPage.cs

public sealed partial class MainPage : Page
{
    public MainPage()
    {
        this.InitializeComponent();

    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        StringBuilder info = new StringBuilder();
        foreach (var item in ViewModel.Items )
        {
            info.AppendLine(item.Ingredient_name + "--------" + item.IsCheck.ToString());
        }
        InfoDisplay.Text = info.ToString();
    }
}

更多内容可以参考Data binding in depth文档。

【讨论】:

    猜你喜欢
    • 2015-06-30
    • 2017-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-02
    • 2013-09-10
    • 2018-08-31
    • 1970-01-01
    相关资源
    最近更新 更多