簡體   English   中英

WPF綁定問題-不起作用?

[英]WPF Binding Problem - Does not work?

我想將代碼隱藏.xaml.cs中的某些屬性綁定到一些xaml代碼中,如下所示:

<TextBlock [someProperties] ... Text="{Binding ElementName=awesome, Path=value}" />
<TextBlock [someProperties] ... Text="{Binding Path=legendary}" />

在關聯的.xaml.cs文件中,我具有以下屬性:

public String legendary = "High five";
public CoolObject awesome = new CoolObject(); //has a public property "String value = '5';"

但是我的TextBlocks只是不想顯示該死的“高五”和“ 5”。 我想念什么?

問題在於,“傳奇”和“令人敬畏”被聲明為字段而非屬性。 WPF綁定不適用於字段。

您需要使用屬性包裝字段。 綁定不支持字段。

所以:

public String _legendary = "High five";
public String legendary {
    get { return _legendary; }
    set { _legendary = value; }
 }

另外,如果您想研究實現INotifyPropertyChanged的方法,以確保更改屬性值時更新與屬性綁定的內容。

並且<TextBlock [someProperties] ... Text="{Binding ElementName=awesome, Path=value}" />將不起作用。 當您想綁定到可視樹屬性中的某些元素時,將使用ElementName。 你需要

<TextBlock [someProperties] ... Text="{Binding Path=awesome.value}" />

另外,您需要將TextBlock的DataContext屬性設置為bject,其中包含需要綁定的屬性。

暫無
暫無

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

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