【问题标题】:Binding doesn't work when using custom control in DataTemplate在 DataTemplate 中使用自定义控件时绑定不起作用
【发布时间】:2012-12-12 10:35:40
【问题描述】:

我正在尝试用自定义控件替换 ListView DataTemplate 中的标准控件,但绑定似乎无法正常工作。下面是 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 是一个可观察类型的集合

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 定义为

    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 中看到了正确的文本,但在自定义控件中却没有。我做错了什么?

【问题讨论】:

    标签: wpf data-binding custom-controls datatemplate


    【解决方案1】:

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

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

    如果您的控件保持如此简单,您可以完全删除CustomText 属性并将&lt;TextBlock Text="{Binding CustomText, Mode=OneWay}" /&gt; 更改为仅&lt;TextBlock Text="{Binding DataItemText} /&gt;

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-02-18
      • 1970-01-01
      • 2017-12-02
      • 2016-03-11
      • 1970-01-01
      • 2019-12-26
      • 1970-01-01
      • 2019-12-10
      相关资源
      最近更新 更多