通过内容控件应用DataTemplate时,不应用视觉状态转换

时间:2019-05-10 11:10:09

标签: .net wpf xaml visualstatemanager

取决于我的按钮“控制模板”的设置方式,“视觉状态”将被应用或不被应用。不幸的是,我无法看到他们工作的机制,因为它不够动态。

通过内容控件和绑定来连接数据模板确实可以使Textblocks Content呈现得很好,当按钮被禁用时,它只是无法处理过渡

<Style x:Key="ItemsControlStyle" TargetType="ItemsControl">
    <Setter Property="ItemsPanel">
        <Setter.Value>
            <ItemsPanelTemplate>
                <WrapPanel />
            </ItemsPanelTemplate>
        </Setter.Value>
    </Setter>

    <Setter Property="ItemTemplate">
        <Setter.Value>
            <DataTemplate DataType="xxx:myType">
                <Button Command="{Binding ActionCommand}" CommandParameter="{Binding Item}" Height="125" Width="200" Margin="10" >
                    <Button.Template>
                        <ContentControl Content="{Binding}" ContentTemplate="{Binding ItemDataTemplateKey, Converter={StaticResource StringToDataTemplateConverter}}" />
                        </ControlTemplate>
                    </Button.Template>
                </Button>
            </DataTemplate>

        </Setter.Value>
    </Setter>

</Style>


<DataTemplate x:Key="ItemTemplate">
    <Border x:Name="Border" BorderThickness="1" >
        <TextBlock x:Name="Textblock" Text="{Binding Caption}" FontFamily="{DynamicResource DefaultFont}"/>

        <VisualStateManager.VisualStateGroups>
            <VisualStateGroup x:Name="CommonStates">
                <VisualState x:Name="Disabled">
                    <Storyboard>
                        <ColorAnimation Storyboard.TargetName="TextBlock" Storyboard.TargetProperty="(Foreground).(SolidColorBrush.Color)" To="Gray" Duration="0:0:0.01"/>
                    </Storyboard>
                </VisualState>
            </VisualStateGroup>
        </VisualStateManager.VisualStateGroups>
    </Border>
</DataTemplate>

当我内联模板时,一切都会按预期运行

<Style x:Key="ItemsControlStyle" TargetType="ItemsControl">
    <Setter Property="ItemsPanel">
        <Setter.Value>
            <ItemsPanelTemplate>
                <WrapPanel />
            </ItemsPanelTemplate>
        </Setter.Value>
    </Setter>

    <Setter Property="ItemTemplate">
        <Setter.Value>
            <DataTemplate DataType="xxx:myType">
                <Button Command="{Binding ActionCommand}" CommandParameter="{Binding Item}" Height="125" Width="200" Margin="10" >
                    <Button.Template>
                        <ControlTemplate TargetType="Button">
                            <Border x:Name="Border" BorderThickness="1" >
                                <TextBlock x:Name="TextBlock" Text="{Binding Caption}" FontFamily="{DynamicResource DefaultFont}"/>

                                <VisualStateManager.VisualStateGroups>
                                    <VisualStateGroup x:Name="CommonStates">
                                        <VisualState x:Name="Disabled">
                                            <Storyboard>
                                                <ColorAnimation Storyboard.TargetName="TextBlock" Storyboard.TargetProperty="(Foreground).(SolidColorBrush.Color)" To="Gray" Duration="0:0:0.01"/>
                                            </Storyboard>
                                        </VisualState>
                                    </VisualStateGroup>
                                </VisualStateManager.VisualStateGroups>
                            </Border>
                        </ControlTemplate>
                    </Button.Template>
                </Button>
            </DataTemplate>
        </Setter.Value>
    </Setter>
</Style>

0 个答案:

没有答案