簡體   English   中英

wpf動態內容綁定

[英]wpf dynamic content 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>

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM