多个按钮的 WPF 样式和模板?

发布于 2024-11-02 14:55:16 字数 776 浏览 7 评论 0原文

我似乎无法获得正确的组合来获得所需的效果:

当前 XAML:

<Button Content="Foo" prism:Click.Command="{Binding FooCommand}"
        Visibility="{Binding IsEnabled, RelativeSource={RelativeSource Self}, Converter={ncon:VisibilityBooleanConverter}}" />
<Button Content="Bar" prism:Click.Command="{Binding BarCommand}"
        Visibility="{Binding IsEnabled, RelativeSource={RelativeSource Self}, Converter={ncon:VisibilityBooleanConverter}}" />

我想提取 Visibility="{Binding IsEnabled,relativeSource={RelativeSource Self}, Converter={ncon:VisibilityBooleanConverter}} " 作为应用于所有按钮(在此 UserControl 资源内)的样式。我似乎无法在这里找到正确的组合来实现这一点。

基本上,它所做的不仅仅是基于 ICommand.CanExecute 禁用按钮,它采用 DependencyProperty 并使用布尔可见性转换器将其绑定到按钮的可见性,因此按钮不仅是残废了,也崩溃了。

I can't seem to get the correct combination to get the desired effect:

Current XAML:

<Button Content="Foo" prism:Click.Command="{Binding FooCommand}"
        Visibility="{Binding IsEnabled, RelativeSource={RelativeSource Self}, Converter={ncon:VisibilityBooleanConverter}}" />
<Button Content="Bar" prism:Click.Command="{Binding BarCommand}"
        Visibility="{Binding IsEnabled, RelativeSource={RelativeSource Self}, Converter={ncon:VisibilityBooleanConverter}}" />

I want to extract out the Visibility="{Binding IsEnabled, RelativeSource={RelativeSource Self}, Converter={ncon:VisibilityBooleanConverter}}" as a style to apply over all the Buttons (within this UserControl resources). I can't seem to get the correct combination going here to make that happen.

Basically, what it does is instead of just disabling the button based on the ICommand.CanExecute it takes that DependencyProperty and binds it to the Visibility of the Button using a boolean-visiblity converter so the button is not only disabled, but also collapsed.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

凡尘雨 2024-11-09 14:55:16

我想风格看起来像这样:

<Style TargetType="{x:Type Button}">
    <Setter Property="Visibility"
            Value="{Binding IsEnabled, RelativeSource={RelativeSource Self}, Converter={ncon:VisibilityBooleanConverter}}"/>
</Style>

这不起作用吗?

Style would look like this i suppose:

<Style TargetType="{x:Type Button}">
    <Setter Property="Visibility"
            Value="{Binding IsEnabled, RelativeSource={RelativeSource Self}, Converter={ncon:VisibilityBooleanConverter}}"/>
</Style>

Doesn't that work?

囍笑 2024-11-09 14:55:16
<Style x:Key="ButtonStyle" TargetType="{x:Type Button}">
    <Style.Triggers>
        <DataTrigger Binding="{Binding IsEnabled, RelativeSource={RelativeSource Self}, Converter={ncon:VisibilityBooleanConverter}}" Value="Visible">
            <Setter Property="Visibility" Value="Visible"/>
        </DataTrigger>
    </Style.Triggers>
</Style>
<Style x:Key="ButtonStyle" TargetType="{x:Type Button}">
    <Style.Triggers>
        <DataTrigger Binding="{Binding IsEnabled, RelativeSource={RelativeSource Self}, Converter={ncon:VisibilityBooleanConverter}}" Value="Visible">
            <Setter Property="Visibility" Value="Visible"/>
        </DataTrigger>
    </Style.Triggers>
</Style>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文