A191919
A191919

Reputation: 3442

wpf dynamic content binding

I want to change label Binding at the runtime. For example when i am clicking on a first button it set binding to FirstLabel Property, when click on second button it change's to SecondLabel. But it update's only one time when click if firing. Is it possible someway to updated it dynamic?

namespace WpfApplication113
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        DataContext = new ReceiveData();
    }
}
public class ReceiveData : INotifyPropertyChanged
{
    private double firstLabel;

    private int secondLabel;
    public double FirstLabel 
    {
        get { return this.firstLabel;}
        set { this.firstLabel = value; NotifyPropertyChanged("FirstLabel"); }
    }
    public int SecondLabel 
    {
        get { return this.secondLabel; }
        set { this.secondLabel = value; NotifyPropertyChanged("SecondLabel"); }
    }
    public ReceiveData()
    {
        Action StartUpdate = new Action(Count);
        IAsyncResult result = StartUpdate.BeginInvoke(null, null);
    }
    public void Count()
    {
        while (true)
        {
            System.Threading.Thread.Sleep(1000);
            FirstLabel += 0.1;
            SecondLabel += 2;
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged(String info)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(info));
        }
    }
}
}

Xaml code:

<Grid>
    <Label Content="{Binding FirstLabel}" HorizontalAlignment="Left" Margin="440,43,0,0" VerticalAlignment="Top"/>
    <Label Content="{Binding SecondLabel}" HorizontalAlignment="Left" Margin="440,71,0,0" VerticalAlignment="Top"/>
    <Label x:Name="TestLabel" Content="Label" HorizontalAlignment="Left" Margin="440,144,0,0" VerticalAlignment="Top"/>
    <Button Content="Button" HorizontalAlignment="Left" Margin="324,49,0,0" VerticalAlignment="Top" Width="75">
        <i:Interaction.Triggers>
            <i:EventTrigger EventName="Click">
                <ei:ChangePropertyAction TargetName="TestLabel" PropertyName="Content" Value="{Binding FirstLabel,UpdateSourceTrigger=PropertyChanged}"/> 
            </i:EventTrigger>
        </i:Interaction.Triggers>
    </Button>
    <Button Content="Button" HorizontalAlignment="Left" Margin="324,77,0,0" VerticalAlignment="Top" Width="75">
        <i:Interaction.Triggers>
            <i:EventTrigger EventName="Click">
                <ei:ChangePropertyAction TargetName="TestLabel" PropertyName="Content" Value="{Binding SecondLabel,UpdateSourceTrigger=PropertyChanged}"/>
            </i:EventTrigger>
        </i:Interaction.Triggers>
    </Button>

</Grid>

Upvotes: 0

Views: 859

Answers (1)

Glen Thomas
Glen Thomas

Reputation: 10744

WPF data binding doesn't work the way you probably imagine.

Instead of using an EventTrigger to handle the Button.Click event, use a command and update the binding source property value instead.

<Label Content="{Binding FirstLabel}"/>
<Label Content="{Binding SecondLabel}"/>
<Label Content="{Binding TestLabel}"/>
<Button Content="Button" Command="{Binding SetTestLabelCommand}" CommandParameter="{Binding FirstLabel}"/>
<Button Content="Button" Command="{Binding SetTestLabelCommand}" CommandParameter="{Binding SecondLabel}"/>

Code-behind:

public ICommand SetTestLabelCommand { get; set; }

string _testLabel;
public string TestLabel
{
    get
    {
        return _testLabel;
    }
    set
    {
        _testLabel = value;
        RaisePropertyChanged("TestLabel");
    }
}

string _firstLabel;
public string FirstLabel
{
    get
    {
        return _firstLabel;
    }
    set
    {
        _firstLabel= value;
        RaisePropertyChanged("FirstLabel");
    }
}

string _secondLabel;
public string SecondLabel
{
    get
    {
        return _secondLabel;
    }
    set
    {
        _secondLabel= value;
        RaisePropertyChanged("SecondLabel");
    }
}

SetTestLabelCommand = new RelayCommand<string>(SetTestLabel);

private void SetTestLabel(string value)
{
    TestLabel = value;
}

Upvotes: 1

Related Questions