【问题标题】:WPF Styling child control based on parent command enabled启用基于父命令的 WPF 样式设置子控件
【发布时间】:2013-03-30 15:56:16
【问题描述】:

我只想让我的Button 模板中的Label 看起来被禁用。搜索了互联网,这似乎是一个简单的任务,我做错了什么?

<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:使用 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>

【问题讨论】:

    标签: c# wpf button styles label


    【解决方案1】:

    请改用ElementNameRelativeSource = FindAncestor 在这里不起作用,因为按钮不在 Visual Tree 中,而是在 StackPanel 的兄弟中。提供name to button 并在您的绑定中使用ElementName -

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

    在 DataTrigger 中 -

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

    Visual Tree 标签和按钮的结构是这样的 -

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

    可以看出 Button 是 StackPanel 的兄弟,它不在 Label 的可视化树中,这就是为什么 FindAncestor 不会让您到达 Button

    【讨论】:

    • 非常感谢,不得不为 True 添加一个“白色”设置器,但效果很好,再次感谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-08-11
    • 2016-07-05
    • 1970-01-01
    • 2016-09-18
    • 1970-01-01
    • 2011-02-12
    • 2021-12-23
    相关资源
    最近更新 更多