Reputation: 77
I am new to Wpf. Basically i achieve Button content Value change dynamically. here is my code.
xaml:
<Button x:Name="btnTest" Content="NO" HorizontalAlignment="Left"VerticalAlignment="Top" Width="233" Height="40" Click="btnTest_Click" />
Cs file:
private void btnTest_Click(object sender, RoutedEventArgs e)
{
if (btnTest.Content.ToString() == "NO")
{
btnTest.Content = "YES";
}
else if (btnTest.Content.ToString() == "YES")
{
btnTest.Content = "NO";
}
}
how can i achieve this same in "Binding" logic.
xaml:
<Button x:Name="btnTest" Content="{Binding ???}" HorizontalAlignment="Left"VerticalAlignment="Top" Width="233" Height="40" Click="btnTest_Click" />
could anyone please help?
Upvotes: 0
Views: 6996
Reputation: 220
Just like this:
Content="{Binding XXX, Mode=TwoWay, ValidatesOnDataErrors=True}"
This will get the value (for Content
in this case) from DataContext
.
EDIT:
A) To create data context (code-behind version):
this.DataContext = new SomeViewModel();
You may put this for example in your constructor (in control/form/whatever)...
B) And finally SomeViewModel
is just a class:
public class SomeViewModel : IDataErrorInfo // sample interface
{
private string _xxx;
public string IP
{
get { return _xxx; }
set { _xxx = value; }
}
}
Upvotes: 3
Reputation: 2619
You can try this
Content= "{Binding RelativeSource={RelativeSource Self}, Path=YNText, Mode=OneWay}"
and add this to the code behind
public string YNText {
get
{
_switch = !_switch;
return _switch ? "YES" : "NO";
}
}
private bool _switch;
Upvotes: 1