C# WPF 数据绑定:根据 var 的内容启用/禁用控件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1049311/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WPF Data Binding : enable/disable a control based on content of var?
提问by Number8
I have a button on my form that should only be enabled when an item is selected in a treeview (or the listview in a tabitem). When an item is selected, it's value is stored in a string member variable.
我的表单上有一个按钮,只有在树视图(或 tabitem 中的列表视图)中选择项目时才应启用该按钮。当一个项目被选中时,它的值存储在一个字符串成员变量中。
Can I bind the IsEnabled
property of the button to the content of the member var? That is, if the member var is not empty, enable the button.
我可以将IsEnabled
按钮的属性绑定到成员 var 的内容吗?也就是说,如果成员 var 不为空,则启用按钮。
Similarly, when the content of the member var changes (set or cleared), the button's state should change.
同样,当成员 var 的内容改变(设置或清除)时,按钮的状态也应该改变。
采纳答案by apandit
Since you're probably looking to bind the IsEnabled property of the button based on a string, try making a converter for it.
由于您可能希望根据字符串绑定按钮的 IsEnabled 属性,因此请尝试为其制作转换器。
Ie...
IE...
<StackPanel>
<StackPanel.Resources>
<local:SomeStringConverter mystringtoboolconverter />
</StackPanel.Resources>
<Button IsEnabled="{Binding ElementName=mytree, Path=SelectedItem.Header, Converter={StaticResource mystringtoboolconverter}}" />
<StackPanel>
and the converter:
和转换器:
[ValueConversion(typeof(string), typeof(bool))]
class SomeStringConverter : IValueConverter {
public object Convert( object value, Type targetType, object parameter, CultureInfo culture ) {
string myheader = (string)value;
if(myhead == "something"){
return true;
} else {
return false;
}
}
public object ConvertBack( object value, Type targetType, object parameter, CultureInfo culture ) {
return null;
}
}
EDIT: Since the OP wanted to bind to a variable, something like this needs to be done:
编辑:由于 OP 想要绑定到一个变量,所以需要做这样的事情:
public class SomeClass : INotifyPropertyChanged {
private string _somestring;
public string SomeString{
get{return _somestring;}
set{ _somestring = value; OnPropertyChanged("SomeString");}
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName)
{
if (this.PropertyChanged != null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
Then, change the above binding expression to:
然后,将上面的绑定表达式更改为:
{Binding Path=SomeString, Converter={StaticResource mystringtoboolconverter}}
Note, you MUST implement INotifyPropertyChanged for your UI to be updated.
请注意,您必须实现 INotifyPropertyChanged 才能更新您的 UI。
回答by Andrej
Do you have a ViewModel holding your string property set as the DataContext of the View where you try to do this Binding?
您是否有一个 ViewModel 持有您的字符串属性设置为您尝试执行此绑定的视图的 DataContext?
Then the following will work:
然后以下将起作用:
// Example ViewModel
public class MyClass : INotifyPropertyChanged
{
private string text;
public string Text
{
get { return text; }
set
{
text = value;
UpdateProperty("Text");
UpdateProperty("HasContent");
}
}
public bool HasContent
{
get { return !string.IsNullOrEmpty(text); }
}
public event PropertyChangedEventHandler PropertyChanged;
protected void UpdateProperty(string name)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(name));
}
}
Then you should have done something like this in the code behind of the view:
那么你应该在视图后面的代码中做这样的事情:
this.DataContext = new MyClass();
And a Xaml example:
和一个 Xaml 示例:
<StackPanel>
<TextBox Text="{Binding Text, UpdateSourceTrigger=PropertyChanged}" />
<Button IsEnabled="{Binding HasContent}">
Click Me!
</Button>
</StackPanel>
回答by Kit
This is not exactlyanswering the question, and yes this is a ten-year-old one, but... I came across this very useful trick, courtesy of https://www.codeproject.com/Tips/215457/How-to-make-Button-Enable-Disable-depends-on-a-Tex.
这并不能完全回答问题,是的,这是一个 10 年前的问题,但是......我遇到了这个非常有用的技巧,由https://www.codeproject.com/Tips/215457/How- 提供to-make-Button-Enable-Disable-depends-on-a-Tex。
<Button Content="Add Name " Width="100" Height="30"
IsEnabled="{Binding ElementName=txtName, Path=Text.Length, Mode=OneWay}">
</Button>
Now the enabledness of the button depends on the populatedness of an element called "txtName".
现在按钮的启用取决于名为“txtName”的元素的填充。