下面是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方法的第一个参数该怎么写呢?似乎不能写啊

解决方案 »

  1.   

    看下这个例子:<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);
    }
    } 
      

  2.   

    报错:无法解析属性路径“Background.Color”中的所有属性引用....
    Background并没有指定为SolidColorBrush,请问下,这个问题该怎么处理。
    你的思路是正确