触发数据触发器不会更改自定义控件的属性(ControlTemplate)

时间:2016-10-18 11:03:53

标签: c# wpf custom-controls controltemplate datatrigger

我想更改DependcyProperty(名称为MessageTemplateProperty),它位于CustomControl中,类型为Controltemplate

但我无法使用datatrigger

为其设置值

但可以使用datatrigger设置另一个在CustomControl和Type为IsShowMessageProperty的DependcyProperty(名为bool)。

谁能解释一下,为什么以及如何解决它。

自定义代码如下:

public class MessageOverLay : ContentControl
{
    static MessageOverLay()
    {
        DefaultStyleKeyProperty.OverrideMetadata(typeof(MessageOverLay),
            new FrameworkPropertyMetadata(typeof(MessageOverLay)));
    }

    #region DependcyProperties

    // Template attached property

    public static readonly DependencyProperty MessageTemplateProperty =
        DependencyProperty.Register("MessageTemplate", typeof(ControlTemplate), typeof(MessageOverLay),
            new FrameworkPropertyMetadata(MessageTemplateChanged));

    public ControlTemplate MessageTemplate
    {
        set{SetValue(MessageTemplateProperty, value);}
        get{return (ControlTemplate)GetValue(MessageTemplateProperty);}
     }

    // IsVisible attached property

    public static readonly DependencyProperty IsShowMessageProperty =
        DependencyProperty.Register("IsShowMessage", typeof(bool), typeof(MessageOverLay),
            new PropertyMetadata(IsShowMessageChanged));

    public bool IsShowMessage
    {
        get{return (bool)GetValue(IsShowMessageProperty);}
        set{SetValue(IsShowMessageProperty, value);}
    }


    #endregion DependcyProperties



}

自定义默认主题Generic.xaml

 <Style TargetType="{x:Type controls:MessageOverLay}">
      <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="controls:MessageOverLay">
                <AdornerDecorator>
                    <Grid>
                        <ContentPresenter  x:Name="PART_Conent"
                                        VerticalAlignment="Stretch"
                                        HorizontalAlignment="Stretch"

                                         Content="{TemplateBinding Content}" />
                        <Control x:Name="Part_MessageControl" Template="{TemplateBinding MessageTemplate}" 
                                 Visibility="{TemplateBinding IsShowMessage,Converter={StaticResource BooleanToVisibilityConverter}}" />
                    </Grid>
                </AdornerDecorator>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Setter Property="MessageTemplate">
        <Setter.Value>
            <ControlTemplate>
                <Grid HorizontalAlignment="Left" VerticalAlignment="Bottom">
                    <Grid Width="150" Height="100" Margin="5  0 0 10">
                        <Rectangle Stroke="Black" Fill="Yellow" RadiusX="6" RadiusY="6" Margin="0 20 0 0" />
                        <TextBlock Text="What are you doing?" Margin="5 25 0 0" />
                        <Button Content="Cancel" Margin="5" VerticalAlignment="Bottom" HorizontalAlignment="Right" />
                        <Button Content="OK" Margin="5" VerticalAlignment="Bottom" HorizontalAlignment="Left" />
                    </Grid>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

我想按照以下方式使用它:

演示XAML:

  <Window ...>
<Window.Resources>
    <ControlTemplate x:Key="GenderPopupTemplate">
        <Grid HorizontalAlignment="Left" VerticalAlignment="Bottom">
            <Grid Width="200" Height="100" Margin=" 5 0 0 10">              
                <TextBlock Text="Please Select Gender " Margin="5 25 0 0" />
                <Button Content="Male" Margin="5" VerticalAlignment="Bottom" HorizontalAlignment="Left"
                        Command="{Binding SelectedGenderCommand}" />
                <Button Content="FeMale" Margin="5" VerticalAlignment="Bottom" HorizontalAlignment="Right"
                        Command="{Binding SelectedGenderCommand}" />
            </Grid>
        </Grid>
    </ControlTemplate>

    <ControlTemplate x:Key="FacePopupTemplate">
        <Grid HorizontalAlignment="Left" VerticalAlignment="Bottom">
            <Grid Width="200" Height="100" Margin="5 10 0 0">
                <TextBlock Text="Do you like your Face,Now?" Margin="5 25 0 0" />
                <Button Content="OK" Margin="5" VerticalAlignment="Bottom" HorizontalAlignment="Left"
                        Command="{Binding OKCommand}" />
            </Grid>
        </Grid>
    </ControlTemplate>
</Window.Resources>

    <controls:MessageOverLay  HorizontalAlignment="Stretch"
                              VerticalAlignment="Stretch"
                              MessageTemplate="{StaticResource GenderPopupTemplate}"
                             IsShowMessage="True">
        <controls:MessageOverLay.Style>
            <Style TargetType="{x:Type controls:MessageOverLay}">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding MessageBoxType}" Value="{x:Static viewModels:MessageBoxTypes.SelectView}">
                        <Setter Property="MessageTemplate" Value="{StaticResource GenderPopupTemplate}"></Setter>
                        <Setter Property="Height" Value="350"/>
                    </DataTrigger>
                    <DataTrigger Binding="{Binding MessageBoxType}" Value="{x:Static viewModels:MessageBoxTypes.MessageView}">
                        <Setter Property="MessageTemplate" Value="{StaticResource FacePopupTemplate}"></Setter>
                        <Setter Property="Height" Value="150"/>
                    </DataTrigger>

                </Style.Triggers>
            </Style>
        </controls:MessageOverLay.Style>
        <Grid  Background="SeaGreen" 
               HorizontalAlignment="Stretch"
                              VerticalAlignment="Stretch">
            <TextBlock Text="Content"/>
        </Grid>
    </controls:MessageOverLay>

我希望在MessageBoxType的{​​{1}}符号发生变化时更改MessageTemplate。 但是我无法归档它。

其他相关代码

演示C#代码:

MainWindowViewModel

更新

Here是github中的完整演示代码,请检查一下。

1 个答案:

答案 0 :(得分:1)

当样式设置器设置依赖项属性时,不必像在

中那样直接分配所谓的本地值。
<controls:MessageOverLay MessageTemplate="{StaticResource GenderPopupTemplate}" ...>

直接分配的本地值始终优先于样式设置器(以及其他可能的源)中的任何值,因此Setter无效。更多细节可以在这里找到:Dependency Property Value Precedence

将直接分配替换为另一个样式设定器:

 <controls:MessageOverLay  ...>
    <controls:MessageOverLay.Style>
        <Style TargetType="{x:Type controls:MessageOverLay}">

            <Setter Property="MessageTemplate"
                    Value="{StaticResource GenderPopupTemplate}"/>

            <Style.Triggers>
                ...
            </Style.Triggers>
        </Style>
    </controls:MessageOverLay.Style>
    ...
</controls:MessageOverLay>