所以我创建了一个带有几个绑定复选框的组合框。我的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>
我的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(&#34; sandwichFilling&#34;)从数据库接收三明治填充物并将它们放入ComboBox内的复选框中。
当用户按下按钮时,我希望程序知道检查了哪些复选框。我怎么能这样做?
答案 0 :(得分:0)
当用户按下按钮时,我希望程序知道检查了哪些复选框。我怎么能这样做?
根据您的要求,您需要为绑定创建数据源。以下是继承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();
}
}
}
然后创建用于绑定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
},
};
}
}
<强>用法强>
<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
文档。