wpf动态内容绑定

时间:2015-08-04 14:22:48

标签: c# wpf xaml

我想在运行时更改标签Binding。例如,当我点击第一个按钮时,它会设置与FirstLabel属性的绑定,当单击第二个按钮时,它会更改为SecondLabel。但是,如果点击,它只会更新一次。是否有可能将其动态更新?

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>

1 个答案:

答案 0 :(得分:1)

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