c# - XAML 绑定(bind)用户控件 TextBlock 不会显示绑定(bind)数据

标签 c# xaml data-binding

我有一个 ListView 控件,它使用 DataTemplate 来绑定(bind)数据。 在我创建依赖关系后,绑定(bind)使用一个简单的字符串,但在我绑定(bind)类属性时将不起作用。如果我将数据绑定(bind)到一个文本 block ,它就可以工作,但如果我将相同的东西绑定(bind)到我的用户控件,它就不起作用。

这是我的 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;
        }

这是我的简单用户类:

public class User {
    public string Name { get; set; }
    public int Age { get; set; }
    public int Playlist { get; set; }
}

所以: 如果我这样做,在 XAML 中:WORKS

  <Views:UserSelectRibbon NameText ="Some text here" /> 

这将起作用并将文本添加到用户控件中的 TextBlock

但是: 在 XAML 中,如果我这样做:DOESN'T"T WORK

<Views:UserSelectRibbon NameText ="{Binding Name}" />

我想知道为什么它在没有绑定(bind)的情况下可以工作,但它不能与绑定(bind)一起工作

最佳答案

问题出在您的 DataContext 上:

public UserSelectRibbon()
{
    InitializeComponent();
    DataContext = this;
}

这有以下含义:

<Views:UserSelectRibbon NameText ="{Binding Name}" />

在这里,DataContext 已经更改为 Views:UserSelectRibbon,因此您无法绑定(bind)到外部 DataContext 的任何内容不再。

解决方案:不要从内部将 UserControlDataContext 设置为其自身。 从不!

相反,将其设置在用户控件树内的元素上或使用一些 RelativeSourceElementName 绑定(bind)。

内部 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 基类型或您特定的用户控件内部类型):

<TextBlock Text="{Binding NameText,RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}"/>

元素名称的解决方案:

<UserControl ...
             x:Name="myControl">
    <Grid>
        <TextBlock Text="{Binding NameText,ElementName=myControl}"/>
    </Grid>
</UserControl>

关于c# - XAML 绑定(bind)用户控件 TextBlock 不会显示绑定(bind)数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44495195/

相关文章:

c# - 为静态类定义全局别名

c# - 当您没有对容器的引用时,是否可以让 CaSTLe Windsor 解决属性依赖关系?

c# - 我如何转换int?进入整数

wpf - 在 xaml 中包含 xaml 元素

c# - WinRT XAML 数据绑定(bind)包含列表的对象列表

c# - 试图制作一个二维列表数组

c# - 无论属性值如何,WPF 动画都会触发

c# - 将 ComboBox DataSource 设置为枚举并绑定(bind) SelectedValue

c# - 为什么 WPF Binding 不调用 PropertyMetadata 中定义的 propertyChangedCallback,而代码隐藏中的 setter 调用?

c# - 如何防止 CurrencyManager 为绑定(bind)对象调用 BeginEdit()/EndEdit() 方法