Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/sharepoint/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 是否选中UWP已绑定复选框?_C#_Xaml_Uwp_Uwp Xaml - Fatal编程技术网

C# 是否选中UWP已绑定复选框?

C# 是否选中UWP已绑定复选框?,c#,xaml,uwp,uwp-xaml,C#,Xaml,Uwp,Uwp Xaml,所以我创建了一个组合框,里面有几个绑定的复选框。我的xaml如下所示: <ComboBox x:Name="CbSandwichFilling" ItemsSource="{x:Bind SandwichFillingList}" PlaceholderText="Choose sandwich filling"> <ComboBox.ItemTemplate> <DataTemplate> <CheckB

所以我创建了一个组合框,里面有几个绑定的复选框。我的xaml如下所示:

<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>
private List<Ingredients> sandwichFilling;

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

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

我的C#看起来像这样:

<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>
private List<Ingredients> sandwichFilling;

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

public BasicSandwiches()
{
    sandwichFilling = Ingredients.GetIngredients("sandwichFilling");
    this.DataContext = SandwichFillingList;
}
私有列表三明治填充;
公共列表三明治填充列表
{
获取{返回三明治填充;}
设置{sandwichFilling=value;}
}
公共基本三明治
{
三明治馅料=配料。获取配料(“三明治馅料”);
this.DataContext=三明治填充列表;
}
功能GetComponents(“三明治填充”)从数据库接收三明治填充,并将其放入组合框内的复选框中

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

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

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

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();
        }
    }
}
然后创建用于绑定xaml代码的
MainPageViewModel

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
            },

        };

    }
}


有关更多信息,请参阅文档。

WPF、UWP和XAML的设计考虑了MVVM模式。虽然您可以使用其他方法,但这样做会损失大约90%的功能,并在其他每个角落遇到问题。这看起来根本不像MVVM。在MVVM模式中,IngredEnts将公开复选框也将绑定的“selected”属性,这使得答案变得微不足道(只需查看bool值)。几年前,我写了一篇关于MVVM的简短介绍,但它仍然可能帮助您继续:理想情况下,您的配料类应该有一个IsSelected属性,您可以将其与复选框的IsChecked属性绑定。现在,当您单击该按钮时,您可以迭代三明治填充列表集合并检查哪些成分的IsSelected属性设置为true。