WPF样式和模板许多按钮?

时间:2011-04-22 17:09:45

标签: wpf binding styles controltemplate relativesource

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

当前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禁用按钮,它使用了一个布尔可见性转换器,并使用布尔可见性转换器将其绑定到按钮的可见性,因此按钮不仅被禁用,但也崩溃了。

2 个答案:

答案 0 :(得分:2)

我认为风格看起来像这样:

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

这不起作用吗?

答案 1 :(得分:1)

<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>