【问题标题】:WinRT DependencyProperty is never setWinRT DependencyProperty 从未设置
【发布时间】:2016-03-22 09:36:04
【问题描述】:

我在自定义UserControl 中的DependencyProperty 有一些问题。

我需要以特定方式显示有关人员的信息。为了实现这一点,我有几个UserControls 接收List<PeopleList>,其中包含(显然)一个或多个人。

让我向您展示我的(简化的)代码,然后我将向您解释我的应用程序的实际行为。

这是我的用户控件:

public abstract class PeopleLine : UserControl
{
    public static readonly DependencyProperty PeopleListProperty =
        DependencyProperty.Register("PeopleList", typeof(List<PeopleModel>), typeof(PeopleLine), new PropertyMetadata(default(List<PeopleModel>)));

    public List<PeopleModel> PeopleList
    {
        get { return (List<PeopleModel>)GetValue(PeopleListProperty); }
        set { SetValue(PeopleListProperty, value); }
    }
}

然后是我的 xaml:

<local:PeopleLine
    x:Class="MyApp.Controls.EventSheet.OnePeople"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:MyApp.Controls.EventSheet"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <Grid
        Margin="0 5"
        VerticalAlignment="Top"
        Height="51">
        <TextBlock
            Grid.Column="1"
            HorizontalAlignment="Center"
            Foreground="Red"
            FontSize="25"
            Text="{Binding PeopleList[0].Name}"/>
    </Grid>
</local:PeopleLine>

这一切都始于我的Page,其中包含一个ItemsControl,一个正确的ItemsSource(我已经检查过了)和一个ItemTemplateSelector(也可以正常工作)。这是选择器使用的DataTemplate 之一:

<DataTemplate x:Key="OnePeople">
    <peoplecontrols:OnePeople
        PeopleList="{Binding LinePeopleList}"/>
</DataTemplate>

我使用了几个在这里并不重要的模型,因为我将代码简化为只包含最重要的信息。

那么,回到我的问题。当用字符串替换选择器的 DataTemplate 中的peoplecontrols:OnePeople 并将LinePeopleList[0].Name 替换为Text 时,我显示了正确的文本,证明我的数据此时是正确的。 问题是当放回我的peoplecontrols:OnePeople 时,我的DependencyProperty 永远不会设置。我在 PeopleList 的 setter 处设置了一个断点,它永远不会触发。

我尝试了几次修改(尤其是在这个post 中给出的那些,所以已经尝试用typeof(object) 替换typeof(List&lt;PeopleModel&gt;))但没有成功。另外,我尝试将我的DependencyProperty 替换为string 并直接在DataTemplate 中发送名称,但仍然没有调用setter...

我现在没有更多想法,也不明白我的代码有什么问题。任何帮助将不胜感激。 提前致谢。

托马斯

【问题讨论】:

    标签: c# xaml user-controls windows-runtime dependency-properties


    【解决方案1】:

    尝试在您的 UserControl 的构造函数中添加以下行,在 InitializeComponent 调用之后:

    (this.Content as FrameworkElement).DataContext = this;
    

    我为此创建了一个示例应用程序。希望它正确地反映了您的情况:

    https://github.com/mikoskinen/uwpusercontrollistdp

    如果您克隆应用并运行它,您会注意到绑定不起作用。但是,如果您取消注释 Datacontext = UserControl 中的这一行,一切都应该正常。这是工作代码:

        public PeopleLine()
        {
            this.InitializeComponent();
    
            (this.Content as FrameworkElement).DataContext = this;
        }
    

    【讨论】:

    • 这个解决方案非常优雅!我发现另一个正在做完全相同的事情,我将我的 UserControl 命名为(例如 x:Name="Parent"),然后在我的根视图中,我将 DataContext 设置为:DataContext="{Binding ElementName=Parent}"。不过感谢您的回答:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-16
    • 1970-01-01
    • 2013-07-29
    • 2011-01-05
    • 1970-01-01
    相关资源
    最近更新 更多