DataTrigger中的引用按钮名称

时间:2018-06-02 01:33:38

标签: c# wpf xaml

我有一个样式按钮如下:

<Style x:Key="btnDefaultOperationImage" TargetType="{x:Type Button}">
    <Setter Property="Foreground" Value="White"/>
    <Setter Property="HorizontalAlignment" Value="Stretch"/>
    <Setter Property="VerticalAlignment" Value="Stretch"/>
    <Setter Property="VerticalContentAlignment" Value="Center"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Button}">
                <Border Name="brdMenu"
                        BorderThickness="0"
                        Padding="4,2"
                        BorderBrush="White"
                        Background="#20000000">
                    <StackPanel>
                        <ContentPresenter x:Name="ButtonContentPresenter" VerticalAlignment="Center" HorizontalAlignment="Center">
                            <ContentPresenter.Resources>
                                <Style TargetType="TextBlock">
                                    <Setter Property="TextAlignment" Value="Center" />
                                </Style>
                            </ContentPresenter.Resources>
                        </ContentPresenter>
                        <Image>
                            <Image.Style>
                                <Style TargetType="{x:Type Image}">
                                    <Setter Property="Source" Value="/Images/Icons/CourtHearing.png" />
                                    <Style.Triggers>
                                        <DataTrigger Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Button}}, Path=Name}" Value="btnCases">
                                            <Setter Property="Source" Value="/Images/Icons/Cases.png"/>
                                        </DataTrigger>
                                    </Style.Triggers>
                                </Style>
                            </Image.Style>
                        </Image>
                    </StackPanel>
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter TargetName="brdMenu" Property="BorderBrush" Value="#F2826A"/>
                        <Setter TargetName="brdMenu" Property="Background" Value="#30000000"/>
                        <Setter TargetName="brdMenu" Property="TextElement.FontSize" Value="25"/>
                        <Setter TargetName="brdMenu" Property="TextElement.Foreground" Value="AntiqueWhite"/>
                    </Trigger>
                    <Trigger Property="IsPressed" Value="True">
                        <Setter TargetName="brdMenu" Property="BorderBrush" Value="OrangeRed" />
                        <Setter TargetName="brdMenu" Property="Background" Value="White" />
                    </Trigger>
                    <Trigger Property="IsEnabled" Value="false">
                        <!--<Setter TargetName="brdMenu" Property="Background" Value="{StaticResource DisabledBackgroundBrush}" />-->
                        <Setter TargetName="brdMenu" Property="Background" Value="#f9f9f9" />
                        <Setter TargetName="brdMenu" Property="Opacity" Value="0.3" />
                        <Setter Property="Foreground" Value="Black" />
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

我想在许多按钮上使用此样式,但每个按钮根据其名称将具有不同的图像,例如:

  • 按钮Name="btnCases" ImageSource="..\..\Cases.png"
  • 按钮Name="btnActors" ImageSource="..\..\Actors.png"

我想知道是否可以使用DataTrigger解决此问题,我试过

<DataTrigger Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Button}}, Path=Name}" Value="btnCases">

但它没有用。

2 个答案:

答案 0 :(得分:1)

我认为你要求这样的事情。您需要做的是bind image source button每个tag的财产。我在此示例中使用了attached property属性,但您可以使用任何内容 - ViewModel property<Window.Resources> <Style x:Key="btnDefaultOperationImage" TargetType="{x:Type Button}"> <Setter Property="Foreground" Value="White"/> <Setter Property="HorizontalAlignment" Value="Stretch"/> <Setter Property="VerticalAlignment" Value="Stretch"/> <Setter Property="VerticalContentAlignment" Value="Center"/> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type Button}"> <Border Name="brdMenu" BorderThickness="0" Padding="4,2" BorderBrush="White" Background="#20000000"> <StackPanel> <ContentPresenter x:Name="ButtonContentPresenter" VerticalAlignment="Center" HorizontalAlignment="Center"> <ContentPresenter.Resources> <Style TargetType="TextBlock"> <Setter Property="TextAlignment" Value="Center" /> </Style> </ContentPresenter.Resources> </ContentPresenter> <Image> <Image.Style> <Style TargetType="{x:Type Image}"> <Setter Property="Source" Value="{Binding Tag, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Button}}}" /> </Style> </Image.Style> </Image> </StackPanel> </Border> <ControlTemplate.Triggers> <Trigger Property="IsMouseOver" Value="True"> <Setter TargetName="brdMenu" Property="BorderBrush" Value="#F2826A"/> <Setter TargetName="brdMenu" Property="Background" Value="#30000000"/> <Setter TargetName="brdMenu" Property="TextElement.FontSize" Value="25"/> <Setter TargetName="brdMenu" Property="TextElement.Foreground" Value="AntiqueWhite"/> </Trigger> <Trigger Property="IsPressed" Value="True"> <Setter TargetName="brdMenu" Property="BorderBrush" Value="OrangeRed" /> <Setter TargetName="brdMenu" Property="Background" Value="White" /> </Trigger> <Trigger Property="IsEnabled" Value="false"> <!--<Setter TargetName="brdMenu" Property="Background" Value="{StaticResource DisabledBackgroundBrush}" />--> <Setter TargetName="brdMenu" Property="Background" Value="#f9f9f9" /> <Setter TargetName="brdMenu" Property="Opacity" Value="0.3" /> <Setter Property="Foreground" Value="Black" /> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style> </Window.Resources> <Grid> <StackPanel> <Button Style="{StaticResource btnDefaultOperationImage}" Tag="..\image1.png"/> <Button Style="{StaticResource btnDefaultOperationImage}" Tag="..\image2.png"/> </StackPanel> </Grid> 等。

import pandas as pd
dataset=pd.read_csv('D:\\filename.csv', header=None, usecols=3,4,10,16,22,28])
time=dataset.iloc[:,0]

答案 1 :(得分:1)

您也可以使用Converter

<强>假设:

1)每个按钮都以Btn

为前缀

2)图像只有一种格式

<Image Source="{Binding RelativeSource={RelativeSource FindAncestor, 
               AncestorType={x:Type Button}},Path=Name,Converter= 
               {StaticResource NameToPathConverter}}"/>

然后你的按钮看起来像:

<Button Name="BtnDestinationPin" Style="{StaticResource btnDefaultOperationImage}" />

将Converter定义为资源:

<local:NameToPathConverter x:Key="NameToPathConverter"/>

代码:

public class NameToPathConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        string imageName = value.ToString().Substring(3);
        return new BitmapImage(new Uri("pack://application:,,,/YourNamespace;component/Images/" + imageName + ".png"));
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}