如何将自定义 UserControl 绑定到 ItemTemplate 内的当前项目?

发布于 2024-12-07 11:32:03 字数 1866 浏览 0 评论 0原文

我有一个 ListBox,它的数据绑定到 ObservableCollection
DataTemplate 内,我有一个想要绑定到当前项目的自定义用户控件。 我该怎么做? 绑定路径应该是什么?

模型

private ObservableCollection<MyItem> _items;
public ObservableCollection<MyItem> Items
{
  get { return _items; }
  set
  {
    if (_items.Equals(value))
      return;
    _items = value;
    RaisePropertyChanged("Items");
  }
}

XAML

<ListBox ItemsSource="{Binding Items}">
  <ListBox.ItemTemplate>
    <DataTemplate>
      <StackPanel Orientation="Horizontal">
        <TextBlock TextWrapping="Wrap" Text="{Binding Id}" Margin="2"/>
        <TextBlock TextWrapping="Wrap" Text="{Binding Name}" Margin="2"/>
        <uc:MyControl MyValue="{Binding <WHATSHOULDTHISBE>, Mode=TwoWay}" Margin="2"/>
      </StackPanel>
    </DataTemplate>
  </ListBox.ItemTemplate>

用户控制

public partial class MyControl : UserControl
{
  public MyItem MyValue
  {
    get { return (MyItem)GetValue(MyProperty); }
    set { SetValue(MyProperty, value); }
  }

  public static readonly DependencyProperty MyProperty = DependencyProperty.Register("MyValue",
                                                        typeof(MyItem), typeof(MyControl),
                                                        new PropertyMetadata(
                                                          new PropertyChangedCallback(PropertyChanged)));
  public MyControl()
  {
    InitializeComponent();
  }

  private static void PropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
  {
    MyControl c = obj as MyControl;
    if (c != null)
    {
      // TODO: do something here 
    }
  }
}

I have a ListBox that is data bound to an ObservableCollection.
Inside the DataTemplate, I have this custom user control that I want to bind to the current item.
How do I do that?
What should the binding path be?

Model:

private ObservableCollection<MyItem> _items;
public ObservableCollection<MyItem> Items
{
  get { return _items; }
  set
  {
    if (_items.Equals(value))
      return;
    _items = value;
    RaisePropertyChanged("Items");
  }
}

XAML:

<ListBox ItemsSource="{Binding Items}">
  <ListBox.ItemTemplate>
    <DataTemplate>
      <StackPanel Orientation="Horizontal">
        <TextBlock TextWrapping="Wrap" Text="{Binding Id}" Margin="2"/>
        <TextBlock TextWrapping="Wrap" Text="{Binding Name}" Margin="2"/>
        <uc:MyControl MyValue="{Binding <WHATSHOULDTHISBE>, Mode=TwoWay}" Margin="2"/>
      </StackPanel>
    </DataTemplate>
  </ListBox.ItemTemplate>

User control:

public partial class MyControl : UserControl
{
  public MyItem MyValue
  {
    get { return (MyItem)GetValue(MyProperty); }
    set { SetValue(MyProperty, value); }
  }

  public static readonly DependencyProperty MyProperty = DependencyProperty.Register("MyValue",
                                                        typeof(MyItem), typeof(MyControl),
                                                        new PropertyMetadata(
                                                          new PropertyChangedCallback(PropertyChanged)));
  public MyControl()
  {
    InitializeComponent();
  }

  private static void PropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
  {
    MyControl c = obj as MyControl;
    if (c != null)
    {
      // TODO: do something here 
    }
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

唯憾梦倾城 2024-12-14 11:32:03

MyControl.DataContext property would already be bound to your item view model in this case.

弥繁 2024-12-14 11:32:03

您的 MyItem 是数据上下文 - 如果您想绑定到 My Item 的某个成员,那么它:

Binding Path=my_item_member 

要完整地绑定到 MyItem 我认为您想要:

Binding Path=.

您可能还需要一个用于控件的 DataTemplate (除了您拥有的ListBoxItems)将 MyItems 成员映射到您的控制字段。

Your MyItem is the data context - if you want to bind to some member of My Item then its:

Binding Path=my_item_member 

To bind to MyItem in its entirety I think you want:

Binding Path=.

Your might also need a DataTemplate for your control (in addition to the you have for the ListBoxItems) that maps the MyItems members to your control fields.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文