ItemsControl 中的数据绑定到自定义 UserControl 属性

发布于 2024-09-10 09:05:36 字数 2228 浏览 1 评论 0原文

我在数据绑定方面遇到了重大问题。

我的 MainPage.xml 中有一个带有 ItemControl 的堆栈面板:

                <StackPanel>
                    <ItemsControl x:Name="TopicList">
                        <ItemsControl.ItemTemplate>
                            <DataTemplate>
                                <local:TopicListItem Title="{Binding Title}"/>
                            </DataTemplate>
                        </ItemsControl.ItemTemplate>
                    </ItemsControl>
                </StackPanel>

然后我将一个 IEnumerable 对象挂接到该对象上,该对象包含一个带有属性 Title 的对象。它是在 MainPage.xaml.cs 中完成的(并且我知道 LINQ 部分正在工作):

var resultStories = from story in resultXML.Descendants("story")
                    select new NewsStory {...};

Dispatcher.BeginInvoke(() => TopicList.ItemsSource = resultStories);

在我的自定义控件 TopicListItem 中,我创建了一个 DepenencyProperty 和相应的公共属性:

    #region Title (DependencyProperty)

    /// <summary> 
    /// Title
    /// </summary> 
    public String Title
    {
        get { return (String)GetValue(TitleProperty); }
        set { SetValue(TitleProperty, value); }
    }
    public static readonly DependencyProperty TitleProperty =
        DependencyProperty.Register("Title", typeof(String), typeof(TopicListItem),
        new PropertyMetadata(0, new PropertyChangedCallback(OnTitleChanged)));

    private static void OnTitleChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        ((TopicListItem)d).OnTitleChanged(e);
    }

    private void OnTitleChanged(DependencyPropertyChangedEventArgs e)
    {
        throw new NotImplementedException();
    }

    #endregion Title (DependencyProperty)

当我运行此命令并它尝试设置 ItemSource 时,Title 属性出现错误:

System.TypeInitializationException:“NewsSync.TopicListItem”的类型初始值设定项引发异常。 ---> System.ArgumentException:默认值类型与类型不匹配 财产

--
附带说明:我尝试不为 Title 属性声明 DepenencyProperty,而只是将其作为公共字符串。但后来我遇到了转换问题,它说我无法从 System.[...].Binding 转换为 System.String

所以我确实尝试了很多东西。

I am having major problem in Data Binding.

I have a stackpanel with an ItemControl in my MainPage.xml:

                <StackPanel>
                    <ItemsControl x:Name="TopicList">
                        <ItemsControl.ItemTemplate>
                            <DataTemplate>
                                <local:TopicListItem Title="{Binding Title}"/>
                            </DataTemplate>
                        </ItemsControl.ItemTemplate>
                    </ItemsControl>
                </StackPanel>

Then I hook a IEnumerable object on to that that contains an object with the property Title on it. It is done in the MainPage.xaml.cs (and I know that the LINQ part is working):

var resultStories = from story in resultXML.Descendants("story")
                    select new NewsStory {...};

Dispatcher.BeginInvoke(() => TopicList.ItemsSource = resultStories);

And inside my custom control TopicListItem I have created a DepenencyProperty and corresponding public property:

    #region Title (DependencyProperty)

    /// <summary> 
    /// Title
    /// </summary> 
    public String Title
    {
        get { return (String)GetValue(TitleProperty); }
        set { SetValue(TitleProperty, value); }
    }
    public static readonly DependencyProperty TitleProperty =
        DependencyProperty.Register("Title", typeof(String), typeof(TopicListItem),
        new PropertyMetadata(0, new PropertyChangedCallback(OnTitleChanged)));

    private static void OnTitleChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        ((TopicListItem)d).OnTitleChanged(e);
    }

    private void OnTitleChanged(DependencyPropertyChangedEventArgs e)
    {
        throw new NotImplementedException();
    }

    #endregion Title (DependencyProperty)

When I run this and it tries to set the ItemSource an error comes up on the Title property:

System.TypeInitializationException: The type initializer for 'NewsSync.TopicListItem threw an exception. ---> System.ArgumentException: Default value type does not match type
of property
.

--
As a side note: I have tried not declaring a DepenencyProperty for the Title property and just having it as a public String. But then I get conversion issues where it says that I cannot convert from System.[...].Binding to System.String

So I have really tried many things.

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

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

发布评论

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

评论(1

梦一生花开无言 2024-09-17 09:05:36

这一点是您的问题: -

 public static readonly DependencyProperty TitleProperty =
    DependencyProperty.Register("Title", typeof(String), typeof(TopicListItem),
    new PropertyMetadata(0, new PropertyChangedCallback(OnTitleChanged)));

请注意 PropertyMetadata 构造函数的第一个参数是依赖项属性的默认值。您已将其注册为 typeof(String),但您使用 Int32 (0) 作为初始值。请改用 null。您也可以只使用:-

public static readonly DependencyProperty TitleProperty =
    DependencyProperty.Register("Title", typeof(String), typeof(TopicListItem), null);

因为当将值分配给 Title 时,您的代码当前将引发异常。如果属性更改时您确实想要执行某些操作,则只需指定 PropertyChangedCallback 即可。

This bit is your problem:-

 public static readonly DependencyProperty TitleProperty =
    DependencyProperty.Register("Title", typeof(String), typeof(TopicListItem),
    new PropertyMetadata(0, new PropertyChangedCallback(OnTitleChanged)));

Note the first parameter of the PropertyMetadata constructor is the default value of the dependency property. You have registered it as a typeof(String) but you are using an Int32 (0) as the initial value. Use null instead. You could also just use:-

public static readonly DependencyProperty TitleProperty =
    DependencyProperty.Register("Title", typeof(String), typeof(TopicListItem), null);

Since your code will throw an exception currently when a value is assigned to Title. You only need to specify a PropertyChangedCallback if you actually have something want to do when the property changes.

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