简体   繁体   English

XAML绑定用户控件TextBlock不会显示绑定数据

[英]XAML Bind a user controls TextBlock will not show binding data

I have a list view control that uses the DataTemplate to bind the the data. 我有一个列表视图控件,它使用DataTemplate绑定数据。 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. 如果我将数据绑定到textBlock它可以工作,但如果我将相同的东西绑定到我的用户控件它不起作用。

Here is my XAML: LISTVIEW 这是我的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>

HERE IS MY USER CONTROL: 这是我的用户控制:

  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: In the XAML if I do this : WORKS 所以:在XAML中,如果我这样做:工作

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

This will work and add the text to the TextBlock in the user control 这将工作并将文本添加到用户控件中的TextBlock

BUT: In the XAML if I do this : DOESN'T"T WORK 但是:在XAML中,如果我这样做:不要“工作

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

I would like to know why it works without the binding but it doesn't work with the binding 我想知道为什么没有绑定它可以工作,但它不适用于绑定

The problem is with your DataContext : 问题出在您的DataContext

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

This has an implication on the following: 这对以下内容有影响:

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

In here, the DataContext is already changed to the Views:UserSelectRibbon , so you can't bind to anything from the outer DataContext anymore. 在这里, DataContext已经更改为Views:UserSelectRibbon ,因此您无法再绑定到外部DataContext任何内容。

Solution: do not set the DataContext of a UserControl to itself from the inside. 解决方案:不要从内部将UserControlDataContext设置为自身。 Never! 决不!

Instead, set it on an element inside the usercontrols tree or use some RelativeSource or ElementName binding. 而是将其设置在usercontrols树内的元素上,或使用一些RelativeSourceElementName绑定。

Solution with inner DataContext: 内部DataContext的解决方案:

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

and

public UserSelectRibbon()
{
    InitializeComponent();
    grid1.DataContext = this; // set DataContext on inner control instead of the usercontrol itself
}

Solution with RelativeSource (you can use UserControl base type or your specific usercontrols inner type): 使用RelativeSource的解决方案(您可以使用UserControl基类型或您的特定usercontrols内部类型):

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

Solution with ElementName: ElementName解决方案:

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

I did something like this recently with an itemsControl. 我最近用itemsControl做了类似的事情。 I'm probably gonna forget everything though. 我可能会忘记一切。 Anyway, there's a simpler way then binding straight from the item itself. 无论如何,有一种更简单的方法,然后从项目本身直接绑定。 Instead, you call the item from within your c# and have the binding there. 相反,你从c#中调用该项目并在那里绑定。 After

Mainpage()
{
InitializeComponent();
}

Still in the brackets, you should add this: 仍然在括号中,你应该添加:

List<SampleItem> = new List<SampleItem>;
items.Add({NameText = Name});
lbusers.ItemsSource = items;

And outside the brackets: 在括号外:

public class SampleItem{
public string Name {get; set;}
}

You can add the items.Add line as many times as you want, and that's how many items will show up. 您可以根据需要添加items.Add行多次,这将显示多少项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM