ItemsControl 中的数据绑定到自定义 UserControl 属性
我在数据绑定方面遇到了重大问题。
我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这一点是您的问题: -
请注意
PropertyMetadata
构造函数的第一个参数是依赖项属性的默认值。您已将其注册为typeof(String)
,但您使用Int32
(0) 作为初始值。请改用null
。您也可以只使用:-因为当将值分配给
Title
时,您的代码当前将引发异常。如果属性更改时您确实想要执行某些操作,则只需指定PropertyChangedCallback
即可。This bit is your problem:-
Note the first parameter of the
PropertyMetadata
constructor is the default value of the dependency property. You have registered it as atypeof(String)
but you are using anInt32
(0) as the initial value. Usenull
instead. You could also just use:-Since your code will throw an exception currently when a value is assigned to
Title
. You only need to specify aPropertyChangedCallback
if you actually have something want to do when the property changes.