Reputation: 12870
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.
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.
Similarly, when the content of the member var changes (set or cleared), the button's state should change.
Upvotes: 13
Views: 65625
Reputation: 2136
This is not exactly answering 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 .
<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".
Upvotes: 1
Reputation: 3344
Since you're probably looking to bind the IsEnabled property of the button based on a string, try making a converter for it.
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:
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.
Upvotes: 12
Reputation: 991
Do you have a ViewModel holding your string property set as the DataContext of the View where you try to do this Binding?
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:
<StackPanel>
<TextBox Text="{Binding Text, UpdateSourceTrigger=PropertyChanged}" />
<Button IsEnabled="{Binding HasContent}">
Click Me!
</Button>
</StackPanel>
Upvotes: 9