在DataTemplate中使用自定义控件时,绑定不起作用 [英] Binding doesn't work when using custom control in DataTemplate

查看:186
本文介绍了在DataTemplate中使用自定义控件时,绑定不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用自定义控件替换ListView DataTemplate中的标准控件,但是绑定似乎无法正常工作。这是ListView的定义:

I'm trying to replace a standard control inside ListView DataTemplate with a custom control, but binding doesn't seem to work properly. Here is the definition of the ListView:

<Grid>
    <ListView ItemsSource="{Binding DataItemsCollection}">
        <ListView.ItemTemplate>
            <DataTemplate>
                <StackPanel>
                    <TextBlock Text="Static text" />
                    <TextBlock Text="{Binding DataItemText}" />
                    <BindingTest:CustomControl CustomText="{Binding DataItemText}" />
                </StackPanel>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
</Grid>

其中DataItemsCollection是可观察类型的集合

Where DataItemsCollection is an observable collection of type

public class DataItemClass : INotifyPropertyChanged
{
    string _dataItemText;
    public string DataItemText
    {
        get { return _dataItemText; }
        set { _dataItemText = value; Notify("DataItemText");  }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    public void Notify(string propertyName)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
}

主窗口代码如下:

public partial class MainWindow : Window
{
    public ObservableCollection<DataItemClass> _dataItemsCollection = new ObservableCollection<DataItemClass>
        { 
            new DataItemClass { DataItemText = "Test one" },
            new DataItemClass { DataItemText = "Test two" },
            new DataItemClass { DataItemText = "Test three" }
        };

    public ObservableCollection<DataItemClass> DataItemsCollection
    {
        get { return _dataItemsCollection; }
    }

    public MainWindow()
    {
        InitializeComponent();

        DataContext = this;
    }
}

自定义控件很简单:

<StackPanel Orientation="Horizontal">
    <Label Content="Data Item:" />
    <TextBlock Text="{Binding CustomText, Mode=OneWay}" />
</StackPanel>

CustomText定义为

With CustomText defined as

    public static DependencyProperty CustomTextProperty = DependencyProperty.Register(
        "CustomText", typeof(string), typeof(CustomControl), new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));

    public string CustomText
    {
        get { return (string)GetValue(CustomTextProperty); }
        set { SetValue(CustomTextProperty, value); }
    }

运行此项目时,我在DataTemplate的第二个TextBlock中看到正确的文本,但不在自定义控件内。我在做什么错?

When I run this project I see correct text in second TextBlock in DataTemplate, but not inside of a custom control. What am I doing wrong?

推荐答案

默认情况下 Binding 是相对于 DataContext ,在您的情况下为 DataItemClass ,但 CustomText 属性在 CustomControl 中声明。您需要指定相对绑定源:

By default Binding is relative to DataContext, which is DataItemClass in your case, but CustomText property is declared in CustomControl. You need to specify relative binding source:

<TextBlock Text="{Binding Path=CustomText,
                          RelativeSource={RelativeSource Mode=FindAncestor,
                                    AncestorType=BindingTest:CustomControl}}" />

如果您的控件将保持如此简单,则可以完全删除 CustomText 属性,然后将< TextBlock Text = {Binding CustomText,Mode = OneWay} /> 更改为 ; TextBlock Text = {Binding DataItemText} />

If your control is going to stay that simple, you can completely remove CustomText property and change <TextBlock Text="{Binding CustomText, Mode=OneWay}" /> to just <TextBlock Text="{Binding DataItemText} />.

这篇关于在DataTemplate中使用自定义控件时,绑定不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆