UserControl 与 DataContext 的 DependencyProperty

发布于 2024-10-15 08:12:27 字数 2271 浏览 2 评论 0原文

我有一个列表框,其中使用用户控件作为数据模板。我的 UserControl 有一个 ViewModel。我的 UserControl 中有一个 DependencyProperty,以便我可以将 ListBox 中的项目绑定到 UserControl。

除非我没有为我的 UserControl 设置任何 DataContext,否则它不起作用。

如何在我的 UC 中使用 DP 和自定义 DataContext?

我的列表框:

<ListBox ItemsSource="{Binding Path=ListItems, Mode=TwoWay}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Grid>
                <local:MyCustomUC MyObject="{Binding Path=.}"/>
             </Grid>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

我的用户控件 XAML:

<UserControl x:Class="UserControlDataTemplate.MyCustomUC"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Height="Auto" Width="Auto">
    <StackPanel Orientation="Horizontal">
        <TextBlock Text="{Binding Path=FromViewModel}" />
        <Button Content="{Binding ElementName=MyObject, Path=FromParent}" />
    </StackPanel>
</UserControl>

我的用户控件 CS:

       public MyClass MyObject
        {
            get { return (MyClass)GetValue(MyObjectProperty); }
            set
            {
                SetValue(MyObjectProperty, value);
            }
        }

        // Using a DependencyProperty as the backing store.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty MyObjectProperty =
        DependencyProperty.Register("MyObject", typeof(MyClass), typeof(MyCustomUC), new PropertyMetadata(null));

        public MyCustomUC()
        {
           InitializeComponent();

           this.DataContext = new MyCustomUCViewModel();
        }

My ViewModel:

    public class MyCustomUCViewModel : DependencyObject, INotifyPropertyChanged
    {
        public String FromViewModel { get; set; }

        public MyCustomUCViewModel()
        {
            this.FromViewModel = Guid.NewGuid().ToString();
        }
        ...
     }

来自列表框的 ItemSource 中的项目类:

public class MyClass : INotifyPropertyChanged
{
    public String FromParent { get; set; }
    ...
}

我做错了什么?

I have a ListBox where I'm using a UserControl as DataTemplate. My UserControl has a ViewModel. I have a DependencyProperty in my UserControl so that I can bind item from my ListBox to my UserControl.

It does not work unless I do not set any DataContext to my UserControl.

How can I use DP and custom DataContext in my UC ?

My ListBox:

<ListBox ItemsSource="{Binding Path=ListItems, Mode=TwoWay}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Grid>
                <local:MyCustomUC MyObject="{Binding Path=.}"/>
             </Grid>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

My UserControl XAML:

<UserControl x:Class="UserControlDataTemplate.MyCustomUC"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Height="Auto" Width="Auto">
    <StackPanel Orientation="Horizontal">
        <TextBlock Text="{Binding Path=FromViewModel}" />
        <Button Content="{Binding ElementName=MyObject, Path=FromParent}" />
    </StackPanel>
</UserControl>

My UserControl CS:

       public MyClass MyObject
        {
            get { return (MyClass)GetValue(MyObjectProperty); }
            set
            {
                SetValue(MyObjectProperty, value);
            }
        }

        // Using a DependencyProperty as the backing store.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty MyObjectProperty =
        DependencyProperty.Register("MyObject", typeof(MyClass), typeof(MyCustomUC), new PropertyMetadata(null));

        public MyCustomUC()
        {
           InitializeComponent();

           this.DataContext = new MyCustomUCViewModel();
        }

My ViewModel:

    public class MyCustomUCViewModel : DependencyObject, INotifyPropertyChanged
    {
        public String FromViewModel { get; set; }

        public MyCustomUCViewModel()
        {
            this.FromViewModel = Guid.NewGuid().ToString();
        }
        ...
     }

Item class in ItemSource from ListBox:

public class MyClass : INotifyPropertyChanged
{
    public String FromParent { get; set; }
    ...
}

What did I do wrong ?

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

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

发布评论

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

评论(1

旧情别恋 2024-10-22 08:12:27

这里您在 MyCustomUC() 中设置 DataContext
相反,您可以像这样设置 DataContext

<vm:YourViewModel x:Name="VModel" IfPropertToSet="{BindingFromExistingDC}"/>  

<ListBox ItemsSource="{Binding Path=ListItems, Mode=TwoWay}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Grid>
                <local:MyCustomUC MyObject="{Binding Path=.}" DataContext="{Binding ElementName=VModel}" />
             </Grid>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

您需要包含名称空间

xmlns:vm="clr-namespace:YourViewModelPath"

Here you are setting the DataContext in the MyCustomUC()
instead you can set DataContext like this

<vm:YourViewModel x:Name="VModel" IfPropertToSet="{BindingFromExistingDC}"/>  

<ListBox ItemsSource="{Binding Path=ListItems, Mode=TwoWay}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Grid>
                <local:MyCustomUC MyObject="{Binding Path=.}" DataContext="{Binding ElementName=VModel}" />
             </Grid>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

you need to include the namespace

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