【发布时间】:2016-04-23 16:16:07
【问题描述】:
我正在尝试使用 C# 和 XAML 创建 Windows 10 UWP 应用程序。 在这一点上,我完全被项目模板中的数据绑定所困扰。
所以基本上,我有一个绑定到 XAML 控件的 ViewModel。
我想将数据模型绑定到自定义控件的属性。问题是有些东西有效,有些无效,我真的不明白为什么。
所以这个绑定有效:
<GridView ItemsSource="{Binding MediaElementId}" >
<GridView.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding}" />
</DataTemplate>
</GridView.ItemTemplate>
</GridView>
但是当我将其他东西放入 TextBlock 时,它就不起作用了。
例如,这些绑定不起作用:
<TextBlock Text="{Binding Path=.}"/>
<controls:CustomControl Test="{Binding Path=., UpdateSourceTrigger=PropertyChanged}" />
<controls:CustomControl Test="{Binding Path=., RelativeSource={RelativeSource TemplatedParent}, Mode=OneWay}"/>
<controls:CustomControl Test="{Binding}"/>
所以,我基本上找到了文档,{Binding} 与 {Binding Path=.} 等价,但在这个例子中,它看起来并不是这样。
您可以自己在一个项目中尝试,我在其中隔离了问题: https://bitbucket.org/MrGreeny/pivottestapp/src/4edee74acfac6dbb4f9f042acaf389cc6fd90a31?at=master
编辑: 该属性是一个非常简单的属性:
private string test;
public string Test
{
get { return null; }
set
{
Debug.WriteLine(value);
//What i think should be here is for example textblock.Text = value
}
}
【问题讨论】:
-
UWP 应用的 Property-path syntax 文章没有提到我们从 WPF 中知道的
Path=.语法。但是不知道为什么<controls:CustomControl Test="{Binding}"/>不起作用,因为如果Test属性,您还没有显示声明。 -
好吧,我尝试了很多 Test 属性的实现。当前是:
private string test;public string Test { get { return null; } set { Debug.WriteLine(value); } } -
不要将其作为评论发布,它的格式不正确。改为编辑您的问题。也就是说,
Test必须是一个依赖属性才能成为绑定的目标。 -
快速查看您的位桶代码,我认为您需要创建一个依赖属性:wpftutorial.net/DependencyProperties.html
-
@CodingGorilla 最好参考Dependency properties overview。这是 UWP,不是 WPF。
标签: c# xaml binding windows-10 uwp