【问题标题】:How to change visual state from scroll viewer in wpf xaml如何从 wpf xaml 中的滚动查看器更改视觉状态
【发布时间】:2016-04-09 12:17:16
【问题描述】:

我正在拔头发试图让它发挥作用。我正在努力学习过渡并有点挣扎。基本上我在制作一个组合框,由一个包含 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;
    }

【问题讨论】:

    标签: wpf scrollviewer eventtrigger visualstatemanager visualstates


    【解决方案1】:

    解决了!!!

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多