当在 DataTemplate 中使用控件时,为什么未设置 UserControl 中的自定义属性?

发布于 2024-09-14 18:26:45 字数 2186 浏览 0 评论 0原文

我有一个具有自定义 DependencyProperty 的 UserControl。当我从 DataTemplate 内部使用 UserControl 时,我无法设置 DependencyProperty 的值。如果我直接在窗口中使用 UserControl,则 DependencyProperty 工作正常。我为冗长的帖子表示歉意,我将代码简化到最低限度,但仍然显示了我的项目中存在的问题。感谢您的帮助,我不知道还能尝试什么。

主窗口 XAML:

<Window ...>
    <Window.Resources>
        <DataTemplate DataType="{x:Type local:TextVM}">
            <local:TextV MyText="I do not see this"/> <!--Instead I see "Default in Constructor"-->
        </DataTemplate>
    </Window.Resources>
    <Grid>
        <Border BorderThickness="5" BorderBrush="Black" Width="200" Height="100" >
            <StackPanel>
                <ContentControl Content="{Binding Path=TheTextVM}"/>
                <local:TextV MyText="I see this"/>
            </StackPanel>
        </Border>
    </Grid>
</Window>

主窗口代码:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        this.DataContext = this;
        TheTextVM = new TextVM();
    }

    public TextVM TheTextVM { get; set; }
}

UserControl XAML:

<UserControl ...>
    <Grid>
        <TextBlock x:Name="textBlock"/>
    </Grid>
</UserControl>

UserControl 代码:

public partial class TextV : UserControl
{
    public TextV()
    {
        InitializeComponent();
        MyText = "Default In Constructor";
    }

    public static readonly DependencyProperty MyTextProperty =
       DependencyProperty.Register("MyText", typeof(string), typeof(TextV),
       new PropertyMetadata("", new PropertyChangedCallback(HandleMyTextValueChanged)));

    public string MyText
    {
        get { return (string)GetValue(MyTextProperty); }
        set { SetValue(MyTextProperty, value); }
    }

    private static void HandleMyTextValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs args)
    {
        TextV tv = d as TextV;
        if(tv != null) tv.textBlock.Text = args.NewValue.ToString();
    }
}

I have a UserControl that has a custom DependencyProperty. When I use the UserControl from inside a DataTemplate, I cannot set the value of the DependencyProperty. If I use the UserControl directly in a window, then the DependencyProperty works fine. I apologize for the long post, I simplified the code to the minimum that still shows the problem I have in my project. Thanks for any help, I don't know what else to try.

Main Window XAML:

<Window ...>
    <Window.Resources>
        <DataTemplate DataType="{x:Type local:TextVM}">
            <local:TextV MyText="I do not see this"/> <!--Instead I see "Default in Constructor"-->
        </DataTemplate>
    </Window.Resources>
    <Grid>
        <Border BorderThickness="5" BorderBrush="Black" Width="200" Height="100" >
            <StackPanel>
                <ContentControl Content="{Binding Path=TheTextVM}"/>
                <local:TextV MyText="I see this"/>
            </StackPanel>
        </Border>
    </Grid>
</Window>

Main Window Code:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        this.DataContext = this;
        TheTextVM = new TextVM();
    }

    public TextVM TheTextVM { get; set; }
}

UserControl XAML:

<UserControl ...>
    <Grid>
        <TextBlock x:Name="textBlock"/>
    </Grid>
</UserControl>

UserControl Code:

public partial class TextV : UserControl
{
    public TextV()
    {
        InitializeComponent();
        MyText = "Default In Constructor";
    }

    public static readonly DependencyProperty MyTextProperty =
       DependencyProperty.Register("MyText", typeof(string), typeof(TextV),
       new PropertyMetadata("", new PropertyChangedCallback(HandleMyTextValueChanged)));

    public string MyText
    {
        get { return (string)GetValue(MyTextProperty); }
        set { SetValue(MyTextProperty, value); }
    }

    private static void HandleMyTextValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs args)
    {
        TextV tv = d as TextV;
        if(tv != null) tv.textBlock.Text = args.NewValue.ToString();
    }
}

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

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

发布评论

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

评论(2

风透绣罗衣 2024-09-21 18:26:45

您在代码中设置的值的优先级高于在 XAML 中设置的任何值。

相反,您应该覆盖派生类型中的默认值

The value that you set in code has a higher priority than any value set in XAML.

Instead, you should override the default value in your derived type.

救赎№ 2024-09-21 18:26:45

我可以重现这个问题,但我不知道是什么原因导致的...显然,如果您从构造函数中删除初始化,它就会起作用。如果默认值是常量,则最好的选择是将其用作依赖项属性的默认值,而不是在构造函数中设置它。

不管怎样,你到底想做什么?难道不能通过绑定ViewModel来实现吗?

I can reproduce the problem, but I have no idea what's causing it... Apparently it works if you remove the initialization from the constructor. If the default value is constant, your best option is to use it as the dependency property's default value, rather than set it in the constructor.

Anyway, what are you trying to do exactly? Couldn't it be achieved by a binding on the ViewModel?

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