DisplayStateBehavior not initially applying state transition in listbox item template

限于喜欢 提交于 2019-12-13 04:43:12

问题


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.

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>

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}" .../>

回答1:


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>

In a separated file MyUserControl.cs

<UserControl ... x.Class="MyNamespace.MyUserControl" ...>
  <Grid>
    <VisualStateManager.VisualStateGroups>
      ... blend goodness ...
    </VisualStateManager.VisualStateGroups>   
  </Grid>
</UserControl>

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}" .../>


来源:https://stackoverflow.com/questions/10076732/displaystatebehavior-not-initially-applying-state-transition-in-listbox-item-tem

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!