[英]How can I bind to the property of a class from the codebehind?
在我的XAML中,我有以下內容:
<TextBlock Text="{Binding Stats.Scores.Team}" Style="{StaticResource Column_Value_Large}" />
我需要能夠在背后的代碼中完整創建該TextBlock。 這是我所擁有的:
foreach (var Stats in player){
var columnHeaderScore = new TextBlock
{
Style = Application.Current.Resources["Column_Value_Large"] as Style,
};
columnHeaderScore.SetBinding(TextBlock.TextProperty, new Binding
{
Path = new PropertyPath("Stats.Scores.Team"),
});
columnHeaderStackPanel.Children.Add(columnHeaderScore);
}
但是,綁定似乎不起作用。 在后台代碼中設置綁定的合適方法是什么?
編輯上下文:我的目標是在背后的代碼大循環內生成一堆這樣的文本框。 請參閱我上面修改過的示例,該示例現在顯示了循環。 由於我想這樣做,所以我認為XAML中沒有任何可行的方法。 我將不得不在后面的代碼中設置綁定。
我認為您以錯誤的方式使用xaml。 為什么不在in the XAML code and bind its
t you set the
TextBlock in the XAML code and bind its
可見性in the XAML code and bind its
to a property or use a
樣式. Then you don
. Then you don
不必在背后的代碼中創建綁定。 編輯:為什么不使用ItemPanel或類似的東西將綁定集合並賦予它一個顯示TextBoxes的DataTemplate?
我知道了。
我的問題是在SetBinding中使用“ Path”而不是“ Source”。 工作代碼如下所示:
foreach (var Stats in player){
var columnHeaderScore = new TextBlock
{
Style = Application.Current.Resources["Column_Value_Large"] as Style,
};
columnHeaderScore.SetBinding(TextBlock.TextProperty, new Binding
{
Source = Stats.Scores.Team,
});
columnHeaderStackPanel.Children.Add(columnHeaderScore);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.