DisplayStateBehavior最初未在列表框项目模板中应用状态转换 [英] DisplayStateBehavior not initially applying state transition in listbox item template
问题描述
我有一个包含视觉状态的列表框项目模板。我有将状态设置为开/关状态的DisplayStateBehaviors。我的实现只有1/2可行。在初始显示时,无论DataContext中的值如何,基本状态均处于活动状态。当DataContext中的数据更改时,正确的状态被激活。
I have a listbox item template that contains visual states. I have DisplayStateBehaviors that set the states to either the on/off states. My implementation only 1/2 works. On initial display, the base state is active, regardless of the value in the DataContext. When the data in the DataContext changes, the proper state is activated.
如何在列表框的初始显示中显示正确的状态。
How do I get the proper state displayed on initial display of the listbox.
出于安全原因,我无法复制/粘贴XAML或视图模型代码。
For security reasons I can not copy/paste XAML or View-Model code.
编辑:我无法复制/粘贴真实代码,下面的框架有望重现该问题。
Edit: While I can't copy/paste the real code, the following skeleton hopefully reproduces the problem.
在全局可见的资源文件中:
In a globally visible resource file:
<DataTemplate x:Key="MyObjectItemTemplate">
<Grid>
<VisualStateManager.VisualStateGroups>
... blend goodness ...
</VisualStateManager.VisualStateGroups>
</Grid>
</DataTemplate>
通过将数据上下文与列表框中的ItemsTemplate属性相关联,将数据上下文传递到数据模板中
The data context is passed into the data template by associating it with the ItemsTemplate attribute of the listbox in the main UI.
<ListBox ... ItemTemplate="{DynamicResource MyObjectItemTemplate}" .../>
推荐答案
我发现了我的问题。如果它对其他人有帮助,我要做的就是将DataTemplate的内容提取到UserControl中。这使一切正常工作。 DataTemplate仍然存在并且仍在使用中,但是它只有一个元素-用户控件。视觉状态都是用户控件的一部分。
I found my issue. In the event it helps others, what I did to fix it was to extract out the guts of the DataTemplate into a UserControl. This made everything work as I expected. The DataTemplate still exists and is still involved, but it only has one element - the user control. The visual states are all part of the user control.
在全局可见的资源文件中:
In a globally visible resource file:
<DataTemplate x:Key="MyObjectItemTemplate">
<MyNamespace:MyUserControl/>
</DataTemplate>
在单独的文件MyUserControl.cs
In a separated file MyUserControl.cs
<UserControl ... x.Class="MyNamespace.MyUserControl" ...>
<Grid>
<VisualStateManager.VisualStateGroups>
... blend goodness ...
</VisualStateManager.VisualStateGroups>
</Grid>
</UserControl>
通过将数据上下文与列表框中的ItemsTemplate属性相关联,将数据上下文传递到数据模板中
The data context is passed into the data template by associating it with the ItemsTemplate attribute of the listbox in the main UI.
<ListBox ... ItemTemplate="{DynamicResource MyObjectItemTemplate}" .../>
这篇关于DisplayStateBehavior最初未在列表框项目模板中应用状态转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!