0

I just want to make the Label inside my Button template look disabled. Scoured the Internet and this seems like a simple task, what I'm doing wrong?

<Button Command="{Binding CommandProcess}">
    <StackPanel Orientation="Horizontal">
        <Image Source="Images\Cloud-Upload.png"/>

        <Label Content="Upload and Process" Foreground="White" VerticalAlignment="Center" FontWeight="Bold" FontSize="18.667" Margin="5,0,0,0">
            <Label.Style>
                <Style TargetType="Label">
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding Path=IsEnabled, RelativeSource={RelativeSource AncestorType={x:Type Button}}}" Value="False">
                            <Setter Property="Foreground" Value="Gray"></Setter>
                            <Setter Property="ToolTip" Value="Please select a record type for each file selected for processing"></Setter>
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </Label.Style>
        </Label>
    </StackPanel>
</Button>

EDIT: (full code after using the answer by RV1987)

<Button Command="{Binding CommandProcess}" x:Name="ProcessButton">
    <StackPanel Orientation="Horizontal">
        <Image Source="Images\Cloud-Upload.png"/>

        <Label Content="Upload and Process" VerticalAlignment="Center" FontWeight="Bold" FontSize="18.667" Margin="5,0,0,0">
            <Label.Style>
                <Style TargetType="Label">
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding Path=IsEnabled, ElementName=ProcessButton}" Value="True">
                            <Setter Property="Foreground" Value="White"></Setter>
                        </DataTrigger>

                        <DataTrigger Binding="{Binding Path=IsEnabled, ElementName=ProcessButton}" Value="False">
                            <Setter Property="Foreground" Value="Gray"></Setter>
                            <Setter Property="ToolTip" Value="Please select a record type for each file selected for processing"></Setter>
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </Label.Style>
        </Label>
    </StackPanel>
</Button>
4

1 回答 1

1

Use ElementName instead. RelativeSource = FindAncestor won't work here since button does not lies in Visual Tree but instead its the sibling of StackPanel. Give name to button and use it in your binding using ElementName -

<Button Command="{Binding CommandProcess}" x:Name="MyButton">

and in DataTrigger -

<DataTrigger Binding="{Binding Path=IsEnabled, ElementName=MyButton}"
             Value="False">
   .....
</DataTrigger>

Visual Tree structure for Label and Button is like this -

Label <--- StackPanel <--- StackPanel's Parent
Button <--- StackPanel's Parent

As it can be seen Button is the sibling of StackPanel and it lies nowehere in the Visual tree of Label that's why FindAncestor won't get you to Button.

于 2013-03-30T15:58:51.657 回答