将combox中的复选框设置为选中的wpf [英] set checkbox in a combox to checked wpf
问题描述
我创建了一个自定义的wpf控件-本质上是带有复选框的combox. combox全部成功绑定到项目列表.
这是我的xaml代码.
< ComboBox Height ="28"; HorizontalAlignment =左"保证金="106,7,0,0&".名称="comboBox1"; VerticalAlignment =顶部"宽度="174". ItemsSource =< {装订名称}">
< ComboBox.ItemTemplate>
< DataTemplate>
< CheckBox Name =" ckabc" Content ="{Binding}" CommandParameter =" {Binding}"/>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
我的代码是这样的:
私有列表< string>名称;
公共列表< string>名称
{
得到{返回名称; }
设置
{
名称=值;
this.OnPropertyChanged(new PropertyChangedEventArgs("Names")));
}
}
名称= new List< string> {未连接传感器",信号质量差",光线过大",未连接前置放大器",更换传感器",检测到干扰",无法使用传感器",
传感器变化"指的是传感器变化". };
this.OnPropertyChanged(new PropertyChangedEventArgs("Names")));
我为每个列表项创建了属性:
公共字符串SensorNotConnected
{
得到
{
返回Names.ElementAt(0);
}
设置
{
this.emuObj.SensorNotConnected(Convert.ToBoolean(value),channelIndex);
}
}
为其他列表项创建属性的方法相同.
我的想法是绑定复选框的Ischecked属性并进行迭代.
但是我该怎么做.用户可以选择一个复选框或多个复选框.
请为此提供一些答案.
Hi,
I have created a custom wpf control- essentially a combox with checkbox. The combox all successfully bound to a list of item.
It's my xaml code.
<ComboBox Height="28" HorizontalAlignment="Left" Margin="106,7,0,0" Name="comboBox1" VerticalAlignment="Top" Width="174" ItemsSource="{Binding Names}">
<ComboBox.ItemTemplate>
<DataTemplate>
<CheckBox Name="ckabc" Content="{Binding}" CommandParameter="{Binding}"/>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
My code is here like this:
private List<string> names;
public List<string> Names
{
get { return names; }
set
{
names = value;
this.OnPropertyChanged(new PropertyChangedEventArgs("Names"));
}
}
Names = new List<string> { "Sensor Not Connected", "Poor Signal Quality", "Excessive Light", "PreAmp Not Connected", "Replace Sensor", "Interference Detected", "Sensor Unusable",
"Sensor Change" };
this.OnPropertyChanged(new PropertyChangedEventArgs("Names"));
I have created property for each list item:
public string SensorNotConnected
{
get
{
return Names.ElementAt(0);
}
set
{
this.emuObj.SensorNotConnected(Convert.ToBoolean(value), channelIndex);
}
}
same way created property for other list item.
My thinking is to bind Ischecked property of checkbox and iterate over.
But how Can I do that. User can select one check box or multiple checkbox.
Please provide some answer for this.
P.S:我正在使用MVVM模型.
P.S: I'm using MVVM model.
vikas sharma AB
vikas sharma AB
推荐答案
维卡斯,
您应该为ComboBox中的每个对象创建一个ViewModel,例如这个:
you should create a ViewModel for each object in your ComboBox like e.g. this:
public class NameModel : INotifyPropertyChanged
{
private bool _isChecked;
public NameModel(string name)
{
Name = name;
}
public string Name { get;private set; }
public bool IsChecked
{
get { return _isChecked; }
set
{
_isChecked = value;
var handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs("IsChecked"));
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
然后,您可以创建用于Window,UserControl或其他任何内容的ViewModel:-):
Then you can create your ViewModel that is used for your Window, UserControl or whatever :-) :
public class MainViewModel
{
private List<string> _names;
public MainViewModel()
{
_names = new List<string> { "Sensor Not Connected", "Poor Signal Quality", "Excessive Light", "PreAmp Not Connected", "Replace Sensor", "Interference Detected", "Sensor Unusable", "Sensor Change" };
NameModels = _names.Select(name => new NameModel(name)).ToList();
}
public List<NameModel> NameModels { get; private set; }
}
并在ComboBox中使用NameModel:
And use the NameModel in the ComboBox:
<ComboBox Height="28" HorizontalAlignment="Left" Margin="106,7,0,0" VerticalAlignment="Top" Width="174" ItemsSource="{Binding NameModels}">
<ComboBox.ItemTemplate>
<DataTemplate>
<CheckBox Name="ckabc" Content="{Binding Name}" IsChecked="{Binding IsChecked,Mode=TwoWay}"/>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
现在,您已经在MainViewModel的列表中拥有了所有IsChecked-States,并且可以对该列表进行任何操作.
Now you've all your IsChecked-States in the List in your MainViewModel and you can do whatever you want with that list.
这篇关于将combox中的复选框设置为选中的wpf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!