如何在wpf xaml

时间:2016-04-09 02:33:35

标签: wpf scrollviewer eventtrigger visualstatemanager visualstates

我正拉着我的头发试图让它发挥作用。我试图学习过渡和挣扎一点。基本上我正在制作一个组合框,由一个包含2行的网格组成。顶行是一个按钮,当单击时,底行打开,显示scrollviewer动态添加的按钮。单击时,底部网格行将折叠。

问题是Click事件似乎没有从滚动查看器中触发,或者它无法在范围内找到视觉状态。 因此,单击Button11时SelectionMode工作正常,但单击项目按钮时没有任何反应。按钮是scrollviewer完美地与他们自己的颜色动画和射击事件等工作

我可以在代码隐藏中找到解决方案,因为我可以使路由点击事件解决没问题,但我没有运气

VisualStateManager.GoToState(gridContent, "HiddenMode", true);

理想情况下,我希望这是一个自定义用户控件,我可以像local:CustomComboBox一样添加,但在此阶段,我在自定义控件中控件内部控件很复杂。

MyButton1只是一个简单的按钮,但有颜色过渡等

当我点击按钮

时,没有异常/错误
<Window.Resources>

    <Storyboard x:Key="sb1">
        <DoubleAnimationUsingKeyFrames Storyboard.TargetName="gridContent" Storyboard.TargetProperty="Height">
            <EasingDoubleKeyFrame KeyTime="0" Value="30" />
            <EasingDoubleKeyFrame KeyTime="0:0:1" Value="160" />
        </DoubleAnimationUsingKeyFrames>
        <DoubleAnimationUsingKeyFrames Storyboard.TargetName="scrItems" Storyboard.TargetProperty="Height">
            <EasingDoubleKeyFrame KeyTime="0" Value="0" />
            <EasingDoubleKeyFrame KeyTime="0:0:1" Value="130" />
        </DoubleAnimationUsingKeyFrames>
    </Storyboard>
    <Storyboard x:Key="sb2">
        <DoubleAnimationUsingKeyFrames Storyboard.TargetName="gridContent" Storyboard.TargetProperty="Height">
            <EasingDoubleKeyFrame KeyTime="0" Value="160" />
            <EasingDoubleKeyFrame KeyTime="0:0:1" Value="30" />
        </DoubleAnimationUsingKeyFrames>
        <DoubleAnimationUsingKeyFrames Storyboard.TargetName="scrItems" Storyboard.TargetProperty="Height">
            <EasingDoubleKeyFrame KeyTime="0" Value="130" />
            <EasingDoubleKeyFrame KeyTime="0:0:1" Value="0" />
        </DoubleAnimationUsingKeyFrames>
    </Storyboard>
</Window.Resources>


<Grid Name="Grid1" Margin="0,22,0,0" RenderTransformOrigin="0.5,0.5">
        <Grid Name="gridContent" HorizontalAlignment="Left" Margin="187,74,0,0" VerticalAlignment="Top" Width="140" Height="30" RenderTransformOrigin="0.5,0.5">
        <VisualStateManager.VisualStateGroups>
            <VisualStateGroup x:Name="VisualStateGroup">
                <VisualState x:Name="SelectionMode" Storyboard="{StaticResource sb1}" />
                <VisualState x:Name="HiddenMode" Storyboard="{StaticResource sb2}" />
            </VisualStateGroup>
        </VisualStateManager.VisualStateGroups>

        <Grid.RowDefinitions>
                <RowDefinition Height="30"/>
                <RowDefinition Height="Auto"/>
            </Grid.RowDefinitions>


        <local1:Button1 Name="Button11" Content="Click" Height="30" Grid.Row="0" Margin="0,0,0,30">
                <i:Interaction.Triggers>
                    <i:EventTrigger EventName="Click">
                        <ei:GoToStateAction TargetName="gridContent" StateName="SelectionMode" />
                    </i:EventTrigger>
                </i:Interaction.Triggers>
            </local1:Button1>
        <ScrollViewer Name="scrItems"  VerticalScrollBarVisibility="Hidden" HorizontalScrollBarVisibility="Hidden" Margin="0" Width="140" Height="0" Grid.Row="1">

            <ItemsControl x:Name="stkItems">

                    <ItemsControl.ItemsPanel>
                        <ItemsPanelTemplate>
                            <StackPanel Orientation="Vertical"/>
                        </ItemsPanelTemplate>
                    </ItemsControl.ItemsPanel>
                    <ItemsControl.ItemTemplate>
                        <DataTemplate>

                        <local1:Button1 Content="Click" Height="30">
                            <i:Interaction.Triggers>
                                <i:EventTrigger EventName="Click">

                                    <ei:GoToStateAction TargetName="gridContent" StateName="HiddenMode" />
                                </i:EventTrigger>
                            </i:Interaction.Triggers>
                        </local1:Button1>
                    </DataTemplate>

                    </ItemsControl.ItemTemplate>
                </ItemsControl>
            </ScrollViewer>
        </Grid>
</Grid>




private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        lstItems = new ObservableCollection<MyButton.Button1>();
        for (int i = 0; i <= 999; i++)
        {
            MyButton.Button1 item1 = new Button1();
            item1.Content = "Item " + i;
            item1.Width = stkItems.Width;
            item1.Height = 30;
            //item1.Click += new RoutedEventHandler(Button_Item_Click);
            lstItems.Add(item1);
        }
        stkItems.DataContext = this.stkItems;
        stkItems.ItemsSource = lstItems;
    }

1 个答案:

答案 0 :(得分:0)

解决!!!

我将visualStateManager移动到根元素 - Grid1,但不知道是否会影响它

private void Button_Item_Click(object sender, RoutedEventArgs e)
    {
        bool g = ExtendedVisualStateManager.GoToElementState(this.Grid1 as FrameworkElement, "HiddenMode", true);
    }