读书人

WPF控件可视状态模板深层次该如何

发布时间: 2013-08-09 15:16:24 作者: rapoo

WPF,控件可视状态,模板深层次该怎么写?
下面是ListBox的模板:
<ControlTemplate TargetType="{x:Type ListBox}">
...
...
</ControlTemplate>

下面是ScrollViewer的模板:
<ControlTemplate x:Key="ScrollViewerControlTemplate1" TargetType="{x:Type ScrollViewer}">
...
...
<VisualStateManager.VisualStateGroups>
...
</VisualStateManager.VisualStateGroups>
</ControlTemplate>


使用:
<ListBox MouseEnter="ColorChangeMouseEvent" MouseLeave="ColorChangeMouseEvent">
<ListBoxItem Content="ListBoxItem"/>
<ListBoxItem Content="ListBoxItem"/>
<ListBoxItem Content="ListBoxItem"/>
</ListBox>

private void ColorChangeMouseEvent(object sender, MouseEventArgs e)
{
if (button2.IsMouseOver)
{
VisualStateManager.GoToState(这里写啥呢, "状态1", true);
}
else
{
VisualStateManager.GoToState(这里写啥呢, "状态2", true);
}
} 


问题:
ListBox模板里面,另外还有ScrollViewer的模板,要想在ListBox中使用ScrollViewer状态,GoToState方法的第一个参数该怎么写呢?似乎不能写啊

[解决办法]

顶一个.........
[解决办法]
看下这个例子:

<Window.Resources>
<ControlTemplate x:Key="ListBoxTemplate" TargetType="{x:Type ListBox}">
<ScrollViewer x:Name="scrollViewer" Template="{DynamicResource ScrollViewerTemplate}">
<ItemsPresenter />


</ScrollViewer>
</ControlTemplate>

<ControlTemplate x:Key="ScrollViewerTemplate" TargetType="{x:Type ScrollViewer}">
<Grid x:Name="grid" Background="white">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="VisualStateGroup">
<VisualState x:Name="state1">
<Storyboard>
<ColorAnimation Storyboard.TargetName="grid"
Storyboard.TargetProperty="Background.Color"
To="Blue" />
</Storyboard>
</VisualState>
<VisualState x:Name="state2">
<Storyboard>
<ColorAnimation Storyboard.TargetName="grid"
Storyboard.TargetProperty="Background.Color"
To="Green" />
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<ScrollContentPresenter x:Name="PART_ScrollContentPresenter"
Content="{TemplateBinding Content}" />
</Grid>
</ControlTemplate>
</Window.Resources>
<Grid>
<ListBox x:Name="listbox1" Template="{DynamicResource ListBoxTemplate}"
MouseEnter="ColorChangeMouseEvent" MouseLeave="ColorChangeMouseEvent">
<ListBoxItem Content="ListBoxItem" />
<ListBoxItem Content="ListBoxItem" />
<ListBoxItem Content="ListBoxItem" />
</ListBox>
</Grid>


private void ColorChangeMouseEvent(object sender, MouseEventArgs e)
{
var scrollViewer = listbox1.Template.FindName("scrollViewer", listbox1) as ScrollViewer;
var grid = scrollViewer.Template.FindName("grid", scrollViewer) as Grid;

if (listbox1.IsMouseOver)
{
VisualStateManager.GoToElementState(grid, "state1", true);
}
else
{
VisualStateManager.GoToElementState(grid, "state2", true);
}
} 

[解决办法]
你是测试的上面的代码吗?如果你改过的话,把你的代码贴上来看下

读书人网 >C#

热点推荐