WPF动态内容绑定

本文关键字:绑定 动态 WPF | 更新日期: 2023-09-27 18:06:13

我想在运行时更改标签绑定。例如,当我点击第一个按钮时,它设置绑定到FirstLabel属性,当点击第二个按钮时,它改变为SecondLabel。但它只在click if触发时更新一次。是否有可能动态更新它?

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代码:

<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>

WPF动态内容绑定

WPF数据绑定并不像你想象的那样工作。

不使用EventTrigger来处理Button.Click事件,而是使用命令并更新绑定源属性值。

<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}"/>

后台代码:

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;
}