如何在按钮控件模板的数据触发器中绑定自定义类对象的属性?

时间:2012-10-22 09:38:23

标签: wpf

切换按钮具有以下样式。

<Style x:Key="SearchDataControlStyle" TargetType="{x:Type ToggleButton}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate >
                    <Grid x:Name="grd" Height="80" Width="120" Margin="5">
                        <Rectangle x:Name="baseColor" RadiusY="15" RadiusX="15" Fill="{TemplateBinding Background}"/>
                        <Rectangle x:Name="Glow" RadiusY="15" RadiusX="15" Opacity="0">
                            <Rectangle.Fill>
                                <RadialGradientBrush GradientOrigin="0.505,1.291" RadiusY="0.853" RadiusX="0.853">
                                    <GradientStop Color="Black"/>
                                    <GradientStop Color="White" Offset="1"/>
                                    <GradientStop Color="#FF00BBFF"/>
                                    <GradientStop Color="#FF939393" Offset="1"/>
                                    <GradientStop Color="#00C1C1C1" Offset="0.996"/>
                                    <GradientStop Color="#A5A4E0EB" Offset="0.28"/>
                                    <GradientStop Color="#53818AD5" Offset="0.543"/>
                                    <GradientStop Color="#7C92B5E0" Offset="0.453"/>
                                </RadialGradientBrush>
                            </Rectangle.Fill>
                        </Rectangle>
                        <Rectangle x:Name="Glass" RadiusY="15" RadiusX="15" Opacity="0.8">
                            <Rectangle.Fill>
                                <LinearGradientBrush EndPoint="0.514,1.158" StartPoint="0.514,-0.101">
                                    <GradientStop Color="Black" Offset="0"/>
                                    <GradientStop Color="White" Offset="1"/>
                                    <GradientStop Color="Black" Offset="0.149"/>
                                    <GradientStop Color="#FFABABAB" Offset="1"/>
                                    <GradientStop Color="#FF7A7A7A" Offset="0.414"/>
                                    <GradientStop Color="#FF909090" Offset="0.61"/>
                                </LinearGradientBrush>
                            </Rectangle.Fill>
                        </Rectangle>
                        <StackPanel Orientation="Horizontal">
                            <Image x:Name="imgDependent" Visibility="Visible" Source="D:\Elektra Main\SearchPanel\DynamicSearchPanel\SearchProvider\Dependent.png" HorizontalAlignment="Left" Height="20" Width="20" Margin="3" VerticalAlignment="Top" />
                            <Image x:Name="imgOptional" Visibility="Visible" Source="D:\Elektra Main\SearchPanel\DynamicSearchPanel\SearchProvider\Optional.png" HorizontalAlignment="Left" Height="20" Width="20" Margin="3" VerticalAlignment="Top" />
                        </StackPanel>
                        <ContentPresenter Content="{Binding Description}"  VerticalAlignment="Center" HorizontalAlignment="Center" />
                    </Grid>
                    <ControlTemplate.Triggers>
                        <Trigger Property="Control.IsMouseOver" Value="True">
                            <Setter TargetName="Glow" Property="Opacity" Value="0.8" />
                        </Trigger>
                        <Trigger Property="ToggleButton.IsChecked" Value="True">
                            <Setter TargetName="baseColor" Property="Fill" Value="Black" />
                        </Trigger>
                        <DataTrigger Binding="{Binding IsDependentElement}" Value="{x:Null}" >
                            <Setter TargetName="imgDependent" Property="Visibility" Value="Collapsed"/>
                        </DataTrigger>
                        <DataTrigger Binding="{Binding IsOptionalElement}" Value="{x:Null}" >
                            <Setter TargetName="imgOptional" Property="Visibility" Value="Collapsed"/>
                        </DataTrigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Setter Property="Control.Foreground" Value="White" />
    </Style>

问题是Togglebutton DataContext是一个自定义类,它具有IsOptionalElement和IsDependentElement属性。我设置了两个不工作的数据触发器?

public class SearchData
{
    public string FieldName { get; set; }

    public string Description { get; set; }

    public SearchDataType Type { get; set; }

    public string Validator { get; set; }

    public object DefaultValue { get; set; }

    public object SelectedValue { get; set; }

    public Dictionary<string, object> ComboBoxItemSource
    {
        get;
        set;
    }

    public int DefaultSelectedIndex { get; set; }

    public bool IsYesOrNo { get; set; }

    public OnValidateText OnValidate { get; set; }

    public string OptionalElement { get; set; }

    public string DependentElement { get; set; }

    public bool IsDisplayed { get; set; }

    public bool HasValue { get; set; }
}

1 个答案:

答案 0 :(得分:0)

我认为您Value可能使用了错误的DataTrigger

IsDependentElementIsOptionalElement是普通的“bool”变量,还是可以为空?

我创建了一个包含“bool”属性的ViewModel,然后将DataTrigger更改为Value =“False”,并在False时折叠图像,并在True时显示。