XAML绑定用户控件TextBlock将不显示绑定数据 [英] XAML Bind a user controls TextBlock will not show binding data
问题描述
我有一个列表视图控件,该控件使用DataTemplate绑定数据。
创建依赖项后,绑定将使用简单的字符串工作,但在绑定class属性时将无法工作。如果我将数据绑定到textBlock,则可以使用,但是如果我将相同内容绑定到我的用户控件,则无法使用。
I have a list view control that uses the DataTemplate to bind the the data. After I created the dependency the binding works with a simple string but will not work when I bind the class property. If I bind the data to a textBlock it works, but if I bind the same thing to my User Control it doesn't work.
这是我的XAML:LISTVIEW
Here is my XAML: LISTVIEW
<ListView x:Name='lbUsers'
Height='370'
Canvas.Left='264'
Canvas.Top='183'
Width='1177'
Background='{x:Null}'>
<ListView.ItemTemplate>
<DataTemplate>
<WrapPanel>
<Views:UserSelectRibbon NameText ="{Binding Name}" />
<Image Width='10' />
</WrapPanel>
</DataTemplate>
</ListView.ItemTemplate>
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel Width="{Binding (FrameworkElement.ActualWidth),
RelativeSource={RelativeSource AncestorType=ScrollContentPresenter}}"
ItemWidth="{Binding (ListView.View).ItemWidth,
RelativeSource={RelativeSource AncestorType=ListView}}"
MinWidth="{Binding ItemWidth, RelativeSource={RelativeSource Self}}"
ItemHeight="{Binding (ListView.View).ItemHeight,
RelativeSource={RelativeSource AncestorType=ListView}}" />
</ItemsPanelTemplate>
</ListView.ItemsPanel>
</ListView>
这里是我的用户控件:
public string NameText
{
get { return (string)GetValue(NameTextProperty); }
set { SetValue(NameTextProperty, value); }
}
public static readonly DependencyProperty NameTextProperty =
DependencyProperty.Register("NameText", typeof(string), typeof(UserSelectRibbon),null);
public UserSelectRibbon()
{
InitializeComponent();
DataContext = this;
}
这里是我的简单用户类别:
HERE IS MY SIMPLE USER CLASS :
public class User {
public string Name { get; set; }
public int Age { get; set; }
public int Playlist { get; set; }
}
SO:
在XAML中,如果我这样做:
SO: In the XAML if I do this : WORKS
<Views:UserSelectRibbon NameText ="Some text here" />
这将起作用并将文本添加到用户控件中的TextBlock中
This will work and add the text to the TextBlock in the user control
但是:
在XAML中,如果我这样做:不要工作
BUT: In the XAML if I do this : DOESN'T"T WORK
<Views:UserSelectRibbon NameText ="{Binding Name}" />
知道为什么没有绑定却可以使用
I would like to know why it works without the binding but it doesn't work with the binding
推荐答案
问题出在您的 DataContext
:
public UserSelectRibbon()
{
InitializeComponent();
DataContext = this;
}
以下内容:
<Views:UserSelectRibbon NameText ="{Binding Name}" />
在这里, DataContext
已更改为 Views:UserSelectRibbon
,因此您无法再绑定外部 DataContext
的任何内容。
In here, the DataContext
is already changed to the Views:UserSelectRibbon
, so you can't bind to anything from the outer DataContext
anymore.
解决方案:请勿从内部将 UserControl
的 DataContext
设置为自身。 从不!
Solution: do not set the DataContext
of a UserControl
to itself from the inside. Never!
相反,请将其设置在usercontrols树内的某个元素上,或使用某些 RelativeSource
或 ElementName
绑定。
Instead, set it on an element inside the usercontrols tree or use some RelativeSource
or ElementName
binding.
内部DataContext解决方案:
Solution with inner DataContext:
<UserControl ...>
<Grid x:Name="grid1">
<TextBlock Text="{Binding NameText}"/>
</Grid>
</UserControl>
和
public UserSelectRibbon()
{
InitializeComponent();
grid1.DataContext = this; // set DataContext on inner control instead of the usercontrol itself
}
使用RelativeSource解决方案(您可以使用 UserControl
基本类型或您特定的用户控件内部类型):
Solution with RelativeSource (you can use UserControl
base type or your specific usercontrols inner type):
<TextBlock Text="{Binding NameText,RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}"/>
使用ElementName的解决方案:
Solution with ElementName:
<UserControl ...
x:Name="myControl">
<Grid>
<TextBlock Text="{Binding NameText,ElementName=myControl}"/>
</Grid>
</UserControl>
这篇关于XAML绑定用户控件TextBlock将不显示绑定数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!