簡體   English   中英

WPF數據綁定到字符串屬性

[英]WPF Data Binding to a string property

我有一個關於數據綁定的問題,我正在努力解決。

我的xaml.cs文件中具有以下屬性:

    private string _stationIdInstruction;

    public event PropertyChangedEventHandler PropertyChanged;

    public string StationIdInstruction
    {
        get { return _stationIdInstruction; }
        set
        {
            _stationIdInstruction = value;
            OnPropertyChanged("StationIdInstruction");
        }
    }

    protected void OnPropertyChanged(string name)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(name));
        }
    }

我如何將TextBlock綁定到StationIdInstructions,以便在我更新StationIdInstructions時將字符串屬性用作其Text並更新TextBlock.Text。

任何幫助表示贊賞。

是的,不要忘記指定綁定上下文。 例如,

<Window ... Name="MyWindow">
  <Grid DataContext="{Binding ElementName=MyWindow, Path=.}">
    <TextBlock Text="{Binding Path=StationIdInstruction}" />

在控件的DataContext上設置StationIdInstructions對象,並像這樣設置TextBlock:

<TextBlock Text="{Binding StationIdInstruction}" />

暫無
暫無

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

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