WPF使用带有DataTriggers的模板

时间:2015-06-07 11:29:19

标签: c# wpf xaml

我有一个按钮,其DataContext附有两个图像 显示的图像绑定到按钮IsEnabled属性 这是按钮的XAML:

<Button Name="SubmitButton" IsEnabled="{Binding IsSubmitEnabled}" Background="Transparent"> 
<Image Name="SubmitButtonImage" Height="50" Width="291" MinHeight="50" MinWidth="291">
    <Image.Style>
        <Style TargetType="{x:Type Image}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding IsSubmitEnabled}" Value="True">

                    <Setter Property="Source" Value="/Resources/startup/Submit_enabled.png"></Setter>
                </DataTrigger>

                <DataTrigger Binding="{Binding IsSubmitEnabled}" Value="False">
                    <Setter Property="Source" Value="/Resources/startup/Submit_disabled.png"></Setter>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Image.Style>
</Image>

现在我没有在DataTrigger值中指定图像,而是想使用文件中定义的模板,如下所示:

        <ControlTemplate x:Key="SubmitEnabledTemplate" TargetType="{x:Type Button}">
        <Border                 
            Height="{TemplateBinding Height}"
            Background="{TemplateBinding Background}"
            BorderBrush="{TemplateBinding BorderBrush}"
            BorderThickness="{TemplateBinding BorderThickness}"
            SnapsToDevicePixels="true">
            <Image Source="/Resources/startup/Submit_enabled.png"></Image>
        </Border>
    </ControlTemplate>

    <ControlTemplate x:Key="SubmitDisabledTemplate" TargetType="{x:Type Button}">
        <Border                 
            Height="{TemplateBinding Height}"
            Background="{TemplateBinding Background}"
            BorderBrush="{TemplateBinding BorderBrush}"
            BorderThickness="{TemplateBinding BorderThickness}"
            SnapsToDevicePixels="true">
            <Image Source="/Resources/startup/Submit_disabled.png"></Image>
        </Border>
    </ControlTemplate>

任何想法如何做到这一点?

2 个答案:

答案 0 :(得分:0)

尝试DataTemplate Trigger,这是一个可能相关的示例:

<ItemsControl ItemsSource="{Binding Path=Groups}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>

            <ContentControl x:Name="cc" Content="{Binding}" ContentTemplate="{DynamicResource ItemTemplate}"/>

            <DataTemplate.Triggers>
                <DataTrigger Binding="{Binding Path=IsLeaf}" Value="False">

                    <Setter TargetName="cc" Property="ContentTemplate" Value="{DynamicResource GroupTemplate}"/>
                </DataTrigger>
            </DataTemplate.Triggers>

        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

请参阅DataTemplate.Triggers属性。

答案 1 :(得分:0)

WPF还可以选择切换控件模板  代码:

    <Window.Resources>
    <ControlTemplate x:Key="SubmitEnabledTemplate" TargetType="{x:Type Button}">
        <Border                 
            Height="{TemplateBinding Height}"
            Background="{TemplateBinding Background}"
            BorderBrush="{TemplateBinding BorderBrush}"
            BorderThickness="{TemplateBinding BorderThickness}"
            SnapsToDevicePixels="true">
            <Image Source="/Resources/startup/Submit_enabled.png"></Image>
        </Border>
    </ControlTemplate>

    <ControlTemplate x:Key="SubmitDisabledTemplate" TargetType="{x:Type Button}">
        <Border                 
            Height="{TemplateBinding Height}"
            Background="{TemplateBinding Background}"
            BorderBrush="{TemplateBinding BorderBrush}"
            BorderThickness="{TemplateBinding BorderThickness}"
            SnapsToDevicePixels="true">
            <Image Source="/Resources/startup/Submit_disabled.png"></Image>
        </Border>
    </ControlTemplate>
</Window.Resources>
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="*" />
        <RowDefinition Height="50" />
    </Grid.RowDefinitions>
    <Button Grid.Row="0">
        <Button.Style>
            <Style TargetType="Button">
                <Setter Property="Template"
                        Value="{StaticResource SubmitEnabledTemplate}"/>
                <Style.Triggers>
                    <DataTrigger Binding="{Binding IsSubmitButtonEnabled}"
                                 Value="False">
                        <Setter Property="Template"
                                Value="{StaticResource SubmitDisabledTemplate}"/>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </Button.Style>
    </Button>   
</Grid>