WPF中基于twoComboBox项的文本框文本
我的应用程序中有两个ComboBox,其中ComboBoxItems为是"和否".
如果组合框中选择的ComboBoxItem均为是",我想将我的文本框的文本分配为清除",如果两个组合中的所选ComboBoxItem均为否"或其中一个或否",则将文本框"的文本分配为未清除"在其他ComboBox中如何在WPF中做到这一点?
我已经用这种方法完成了,但是无法正常工作
在我的Xaml中
I have a Two ComboBox in my application in which ComboBoxItems are "Yes" and "No".
I want to assign the text of my TextBox as "cleared" if the selected ComboBoxItem is "Yes" in both the combobox and "not cleared" if the selected ComboBoxItem is "No" in both or "yes" in one or "No" in other ComboBox How do I do that in WPF?
I have Done this way but its not working accuretly
in my Xaml
<ComboBox Height="17" HorizontalAlignment="Left" IsEditable="False" IsReadOnly="False" Margin="297,103,0,0" Name="comboBox1" VerticalAlignment="Top" Width="101" SelectionChanged="comboBox1_SelectionChanged"/>
<ComboBox Height="17" HorizontalAlignment="Left" IsEditable="False" IsReadOnly="False" Margin="297,120,0,0" Name="comboBox2" VerticalAlignment="Top" Width="101" SelectionChanged="comboBox2_SelectionChanged"/>
<TextBox Height="19" HorizontalAlignment="Left" Margin="297,0,0,54" Name="textBox32" VerticalAlignment="Bottom" Width="101" />
Into my Xmal.Cs
{
InitializeComponent();
comboBox1.Items.Add("Yes");
comboBox1.Items.Add("No");
comboBox2.Items.Add("Yes");
comboBox2.Items.Add("No");
}
private void comboBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
{
textBox32.Text = (comboBox1.SelectedItem != null && comboBox1.SelectedItem.ToString() == "Yes") ?
"cleared" :
"not cleared";
}
}
private void comboBox2_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
{
textBox32.Text = (comboBox2.SelectedItem != null && comboBox2.SelectedItem.ToString() == "Yes" ) ?
"cleared" :
"not cleared";
}
}
WPF具有如此强大的绑定功能,因此您不必像上面那样处理控件事件.这里是一个快速汇总的示例,向您显示了您可以做什么(请记住,我没有做太多错误检查,不区分大小写的比较等-代码只是您的一个示例):
WPF has such powerful binding capabilities that you should not have to handle control events as you do above. Here''s a quickly put together example that shows you what you can do (please keep in mind that I have not done too much error checking, case-insensitive compare etc. - the code is just an example for you):
<ComboBox ItemsSource="{Binding Strings}" SelectedItem="{Binding Combo1String}"
Height="23" HorizontalAlignment="Left" Margin="36,35,0,0"
Name="comboBox1" VerticalAlignment="Top" Width="120" />
<ComboBox ItemsSource="{Binding Strings}" SelectedItem="{Binding Combo2String}"
Height="23" HorizontalAlignment="Left" Margin="202,35,0,0"
Name="comboBox2" VerticalAlignment="Top" Width="120" />
<TextBox Text="{Binding Status}"
Height="23" HorizontalAlignment="Left" Margin="42,90,0,0"
Name="textBox1" VerticalAlignment="Top" Width="257" />
public MainWindow()
{
InitializeComponent();
this.Strings = new[] { "yes", "no" };
this.PropertyChanged += MainWindow_PropertyChanged;
}
void MainWindow_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName.StartsWith("Combo"))
{
if (this.Combo1String == "yes" & this.Combo1String == this.Combo2String)
{
this.Status = "Cleared";
}
else
{
this.Status = "Not cleared";
}
}
}
public string[] Strings { get; set; }
private string combo1String;
public string Combo1String
{
get { return combo1String; }
set
{
if (combo1String != value)
{
combo1String = value;
this.FirePropertyChanged("Combo1String");
}
}
}
private string combo2String;
public string Combo2String
{
get { return combo2String; }
set
{
if (combo2String != value)
{
combo2String = value;
this.FirePropertyChanged("Combo2String");
}
}
}
private string status;
public string Status
{
get { return status; }
set
{
if(status != value)
{
status = value;
this.FirePropertyChanged("Status");
}
}
}
~~~~~~~
为了回应您的评论,您需要实现INotifyPropertyChanged
.这是MVVM方法的绝对要求.
~~~~~~~
In response to your comment, you need to implement INotifyPropertyChanged
. That''s an absolute requirement for MVVM approaches.