DataTemplate 数据触发器仅从第二次开始起作用
我有以下 XAML:
<Grid x:Name="root">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid.Resources>
<DataTemplate DataType="{x:Type ViewModels:TemplateViewModel}">
<ContentControl Content="{Binding}" Grid.Row="0" x:Name="ct">
<ContentControl.ContentTemplate>
<DataTemplate>
<TextBlock Text="Loaded" />
</DataTemplate>
</ContentControl.ContentTemplate>
</ContentControl>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding DataContext.State,
RelativeSource={RelativeSource AncestorType={x:Type Window}}}" Value="2">
<Setter Property="ContentTemplate" TargetName="ct">
<Setter.Value>
<DataTemplate>
<TextBlock Text="Loading, please wait" Foreground="Red"/>
</DataTemplate>
</Setter.Value>
</Setter>
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
</Grid.Resources>
<ContentControl Content="{Binding MainContent}" />
该 XAML 位于 Window 元素内。 我将 Window 绑定到具有两个属性(State 和 MainContent)的 ViewModel 对象:
public class ViewModel : INotifyPropertyChanged {
public int State {...} // this can be only 1 or 2, for simplicity
public TemplateViewModel MainContent { ... }
}
我相应地从属性设置器引发 PropertyChanged 事件。
现在,通过一个按钮,我从磁盘加载一个文件,解析它并创建一个对象来分配给 MainContent 属性。 在解析之前,我将 State 属性设置为 2(正在加载),在分配之后,我将属性重置为 1(已加载)。
第一次解析文件时,数据模板中的触发器不起作用(注意触发器绑定到父窗口的数据上下文的 State 属性,即 ViewModel 对象)。 但第二次就这样了!
有人能指出错误在哪里吗?
恐怕我无法在这里发布代码,但如果您有答案并给我发电子邮件,可以分享它。
I have the following XAML:
<Grid x:Name="root">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid.Resources>
<DataTemplate DataType="{x:Type ViewModels:TemplateViewModel}">
<ContentControl Content="{Binding}" Grid.Row="0" x:Name="ct">
<ContentControl.ContentTemplate>
<DataTemplate>
<TextBlock Text="Loaded" />
</DataTemplate>
</ContentControl.ContentTemplate>
</ContentControl>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding DataContext.State,
RelativeSource={RelativeSource AncestorType={x:Type Window}}}" Value="2">
<Setter Property="ContentTemplate" TargetName="ct">
<Setter.Value>
<DataTemplate>
<TextBlock Text="Loading, please wait" Foreground="Red"/>
</DataTemplate>
</Setter.Value>
</Setter>
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
</Grid.Resources>
<ContentControl Content="{Binding MainContent}" />
That XAML is inside a Window element. I'm binding the Window to a ViewModel object with two properties, State and MainContent:
public class ViewModel : INotifyPropertyChanged {
public int State {...} // this can be only 1 or 2, for simplicity
public TemplateViewModel MainContent { ... }
}
I raise the PropertyChanged event from the property setters accordingly.
Now, with a button I load a file from disk, parse it and create an object to assign to the MainContent property. Before parsing I set the State property to 2 (loading) and after assigning I reset the property to 1 (loaded).
The first time I parse the file, the trigger in the data template doesn't work (notice the trigger is bound to the State property of the Data Context of the parent Window, that is, the ViewModel object). But the second time, it does!
Can somebody point where's the error?
I'm afraid I cannot post the code here, but could share it if you have an answer and give me an email..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的
DataTemplate
应用于类型TemplateViewModel
而不是ViewModel
。 因此,在设置MainContent
属性之前,它不会应用于任何内容。Your
DataTemplate
is applied to typeTemplateViewModel
instead ofViewModel
. Ergo, it won't apply to anything until theMainContent
property is set.