繁体   English   中英

将集合绑定到自定义控件属性

[英]Binding a collection to a custom control property

我没有运气尝试将数据集合绑定到我的自定义控件的属性。 我已经实现了这个控件的字符串属性的机制(从这里得到了一些帮助),并希望集合类型也能如此简单。 但是我不能让它再次工作。

这是我的自定义控件视图

<UserControl x:Class="BadaniaOperacyjne.Controls.Matrix"
            mc:Ignorable="d" Name="CustomMatrix"
            d:DesignHeight="300" d:DesignWidth="300">
    <Grid>
        <Grid.RowDefinitions>
            <!-- ... -->
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <!-- ... -->
        </Grid.ColumnDefinitions>
        <TextBlock Grid.Row="0" Grid.Column="1" Text="{Binding ElementName=CustomMatrix, Path=Title}"/>
        <Grid Grid.Row="2" Grid.Column="1" Name="contentGrid">
            <ListBox ItemsSource="{Binding ElementName=CustomMatrix, Path=ItemsList}">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding}"/>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
        </Grid>
    </Grid>
</UserControl>

及其代码隐藏

#region ItemsList Property
public static readonly DependencyProperty ItemsListProperty =
    DependencyProperty.Register("ItemsList", typeof(ObservableCollection<object>), typeof(Matrix), new PropertyMetadata(new ObservableCollection<object>(), new PropertyChangedCallback(ItemsListChanged)));
public ObservableCollection<object> ItemsList
{
    get { return GetValue(ItemsListProperty) as ObservableCollection<object>; }
    set { SetValue(ItemsListProperty, value); }
}
private void ItemsListChanged(object value)
{
    System.Diagnostics.Debug.WriteLine("matrix: items list changed " + value);
    if (ItemsList != null)
    {
        ItemsList.CollectionChanged += ItemsList_CollectionChanged;
        System.Diagnostics.Debug.WriteLine("got " + string.Join(",", ItemsList.ToList()));
    }
    else
    {
        System.Diagnostics.Debug.WriteLine("got null");
    }
}

void ItemsList_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
    System.Diagnostics.Debug.WriteLine("matrix: current items list collection changed");
}
private static void ItemsListChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
    // a breakpoint
    ((Matrix)d).ItemsListChanged(e.NewValue);
}
#endregion

// showing the Title property implementation just to state that
// it is done the same way as for ItemsList
#region Title Property
public static readonly DependencyProperty TitleProperty = 
    DependencyProperty.Register("Title", typeof(string), typeof(Matrix), new PropertyMetadata("", new PropertyChangedCallback(TitleChanged)));
public string Title
{
    get { return (string)GetValue(TitleProperty); }
    set { SetValue(TitleProperty, value); }
}
private void TitleChanged(string title)
{
    System.Diagnostics.Debug.WriteLine("matrix: title changed to: " + title);
}
private static void TitleChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
    ((Matrix)d).TitleChanged((string)e.NewValue);
}
#endregion

这就是我试图绑定到该控件的方式

<custom:Matrix x:Name="customMatrix" DockPanel.Dock="Top" Title="{Binding Title}" ItemsList="{Binding Items}"/>

主页的代码隐藏是

//internal ObservableCollection<List<int>> ItemsList { get; set; }
public class ViewModel : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    public ViewModel()
    {
        Items = new ObservableCollection<int> { 1, 2, 3, 4, 5, 6};
    }

    void Items_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
    {
        System.Diagnostics.Debug.WriteLine("problem manager: items list changed " + e.NewItems.Count);
    }

    public ObservableCollection<int> Items { get; set; }

    protected string title;
    public string Title
    {
        get { return title; }
        set
        {
            if (title != value)
            {
                title = value;
                NotifyPropertyChanged("Title");
            }
        }
    }

    protected void NotifyPropertyChanged(string name)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(name));
        }
    }
}

public ViewModel VM { get; private set; }

// this is the window constructor
private ProblemManager() 
{
    VM = new ViewModel();

    DataContext = VM;
    InitializeComponent();

    VM.Title = "title";
}

private int i = 0;
private void btnAddRow_Click(object sender, RoutedEventArgs e)
{
    // when doing either of these two lines below, 
    // the control breakpoint is never hit
    VM.Items.Add(++i);
    VM.Items = new ObservableCollection<int> { 2, 3 };

    // however, when directly assigning to the control's property, 
    // the event is raised and the breakpoint is hit and the UI is updated
    customMatrix.ItemsList = new ObservableCollection<object> { 1, 2, 3 };
    customMatrix.ItemsList.Add(66);
    // and this of course makes the control's title update
    VM.Title = (++i).ToString();
}

我相信,控件的TitleItemsList DependencyProperty都是以相同的方式创建的。 尽管如此,绑定可能无法正常工作,因为该绑定不会引发ItemsListChanged事件。 因此,问题是我无法通过 XAML 将窗口的ViewModel.Items集合绑定到控件的ItemsList集合。 正在创建一个DependencyProperty控制之内的收集来自任何不同DependencyProperty一个简单的string属性?

问题在于您的DependencyProperty注册。 Co-variance is not applicable for generic lists即您不能这样做 -

ObservableCollection<object> objects = new ObservableCollection<int>();

您已将 DP 类型声明为ObservableCollection<object>但将其与ObservableCollection<int>类型的列表绑定。

您应该change either type of DP to ObservableCollection<int>change binding collection type to ObservableCollection<object>

public ViewModel()
{
    Items = new ObservableCollection<object> { 1, 2, 3, 4, 5, 6};
}

public ObservableCollection<object> Items { get; set; }

经过这么长时间,我有点需要它以两种方式工作:

  1. 能够定义任何类型的项目的集合
  2. 在添加/删除项目等时收到通知。

为了更精确,我正在研究像控制一样的面包屑。

实际上,ItemsSource DP 是用 IList 类型定义的,以允许上述“通用性”:

public static readonly DependencyProperty ItemsSourceProperty =
  DependencyProperty.Register("ItemsSource", typeof(IList), typeof(BreadCrumbUserControl), 
    new PropertyMetadata(null, new PropertyChangedCallback(OnItemsSourceChanged)));

这是我如何使用我的面包屑:

<views:BreadCrumbUserControl ItemsSource="{Binding Items}" SelectedItem="{Binding SelectedItem, Mode=TwoWay}"/>

其中 Items 是自定义类型的 ObservableCollection:

public ObservableCollection<BaseItem> Items { get; set; }

实际上它工作正常,我的 ItemsSource 本地属性指向正确的集合,但是当一个项目添加到 ObservableCollection 时,我需要在我的 UserControl 中得到通知。

所以,我在我的 ItemsSource DP 上使用了 PropertyChangedCallback 委托。

private static void OnItemsSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
  BreadCrumbUserControl thisUserControl = (BreadCrumbUserControl)d;
  (e.NewValue as INotifyCollectionChanged).CollectionChanged += thisUserControl.BreadCrumbUserControl_CollectionChanged;
}

这是对我有用的技巧,因为我的输入引用实际上是在上层定义的 ObservableCollection。

现在这两个目标都已实现:处理任何自定义类型和相同类型的集合,随时了解情况。

如果设置了协议,反射可用于更改我们自定义类型的值。

这只是一个迭代我的集合的例子,它在我的 UserControl 的世界中有一些未知的类型:

foreach (var baseItem in ItemsSource)
{
   baseItem.GetType().GetProperty("IsActive").SetValue(baseItem, false);
}

暂无
暂无

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

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