基于ComboBox选择的WPF按钮IsEnabled覆盖默认样式

时间:2008-12-16 16:44:56

标签: wpf xaml styles isenabled

我有一个Button正在查看2个组合框,以确保它们在启用之前有一个值。问题是我这样做的方式是覆盖我的主题项目中声明的默认样式。

<Button x:Name="btnOK" VerticalAlignment="Center" Content="OK" IsDefault="True"  Margin="0" Click="btnOK_Click">
                    <Button.Style>
                      <Style BasedOn="{StaticResource DefaultButton}">
                            <Style.Triggers>
                                <DataTrigger Binding="{Binding ElementName=ddlWageTypes, Path=SelectedItem}" Value="{x:Null}">
                                    <Setter Property="Button.IsEnabled" Value="false"/>
                                </DataTrigger>
                                <DataTrigger Binding="{Binding ElementName=ddlJobTitles, Path=SelectedItem}" Value="{x:Null}">
                                    <Setter Property="Button.IsEnabled" Value="false"/>
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                    </Button.Style>
                </Button>

我尝试在样式标记中添加BasedOn =“{StaticResouce MyDefaultButtonStyleName}”,但它在运行时会爆炸。

错误是“'System.Windows.Style'值不能分配给对象'System.Windows.Controls.Button'的属性'Style'。只能基于具有基类型'IFrameworkInputElement的目标类型的Style '。标记文件“

中的对象'System.Windows.Style'出错

是否有人在XAML中执行此操作而不覆盖默认样式。

编辑:代码示例已更新。 我在OKButtonStyle上收到一条错误,说“无法将元素添加到属性'资源',因为如果它使用显式集合标记,则该属性只能有一个子元素。标记文件中对象'System.Windows.Style'出错”

<UserControl x:Class="UK.Budgeting.XBAP.ShiftDiff.NewFTEPremiumPaySummary"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:compModel="clr-namespace:System.ComponentModel;assembly=WindowsBase"
    xmlns:local="clr-namespace:UK.Budgeting.XBAP.ShiftDiff">
    <UserControl.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="CellTemplates.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>

      <Style TargetType="{x:Type Button}" x:Key="OKButtonStyle" BasedOn="{StaticResource DefaultButton}">
        <Style.Triggers>
          <DataTrigger Binding="{Binding ElementName=ddlWageTypes, Path=SelectedItem}" Value="{x:Null}">
            <Setter Property="Button.IsEnabled" Value="false"/>
          </DataTrigger>
          <DataTrigger Binding="{Binding ElementName=ddlJobTitles, Path=SelectedItem}" Value="{x:Null}">
            <Setter Property="Button.IsEnabled" Value="false"/>
          </DataTrigger>
        </Style.Triggers>
      </Style>

    </UserControl.Resources>
    <Grid>
        <Rectangle Style="{StaticResource DialogRectangle}"/>
        <Border Style="{StaticResource DialogBorder}">
            <Grid HorizontalAlignment="Center" VerticalAlignment="Center" Background="White">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition/>
                    <ColumnDefinition Width="5"/>
                    <ColumnDefinition MinWidth="300"/>
                </Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                    <RowDefinition/>
                    <RowDefinition Height="2"/>
                    <RowDefinition/>
                    <RowDefinition Height="2"/>
                    <RowDefinition/>
                    <RowDefinition Height="2"/>
                    <RowDefinition/>
                </Grid.RowDefinitions>

                <TextBlock Grid.Column="0" Grid.Row="0" Style="{StaticResource LabelStyle}">Wage Type</TextBlock>
                <TextBlock Grid.Column="0" Grid.Row="2" Style="{StaticResource LabelStyle}">Job Title</TextBlock>

                <ComboBox x:Name="ddlWageTypes" VerticalAlignment="Top" Grid.Column="2" Grid.Row="0"
                          DisplayMemberPath="DisplayName"
                          SelectedValuePath="WageTypeCode"/>
                <ComboBox x:Name="ddlJobTitles" VerticalAlignment="Top" Grid.Column="2" Grid.Row="2"
                          DisplayMemberPath="JobTitle"
                          SelectedValuePath="JobCode"/>

                <StackPanel Grid.Column="2" Grid.Row="6" VerticalAlignment="Top" Orientation="Horizontal" Margin="5">
                  <Button x:Name="btnOK" VerticalAlignment="Center" Content="OK" IsDefault="True"  Margin="0" Click="btnOK_Click" Style="{StaticResource OKButtonStyle}"/>
                    <Button x:Name="btnCancel" VerticalAlignment="Center" Content="Cancel" IsCancel="True" Margin="10,0,0,0" Click="btnCancel_Click"/>
                </StackPanel>
            </Grid>
        </Border>
    </Grid>
</UserControl>

1 个答案:

答案 0 :(得分:10)

这是怎么回事?

BasedOn="{StaticResouce DefaultButton}"

应该引用默认按钮样式?这会崩溃,因为DefaultButton是您应用中未定义的资源。

应该是:

BasedOn="{StaticResource {x:Type Button}}"
编辑:对不起,匆匆回答。

我注意到你的按钮设置了Style = {},并指向一个名为OkBUttonStyle的样式。这是应该定义所有内容并基于默认按钮样式的样式。通过一切,我包括那些触发器。你在XAML中说的是Style是你Button的内容。

也许有些代码会有所帮助:

 <Window x:Class="WindowsApplication7.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="WindowsApplication7" Height="300" Width="300"
    >
  <Window.Resources>
    <Style TargetType="{x:Type Button}" x:Key="defaultButtonStyle">
      <Setter Property="Background" Value="Red" />
    </Style>

    <Style TargetType="{x:Type Button}" x:Key="okButtonStyle" BasedOn="{StaticResource defaultButtonStyle}">
      <Setter Property="Foreground" Value="Green" />
      <Style.Triggers>
        <Trigger Property="IsEnabled" Value="True">
          <Setter Property="Background" Value="Yellow" />
        </Trigger>
        <Trigger Property="IsEnabled" Value="False">
          <Setter Property="Foreground" Value="Blue" />
        </Trigger>
      </Style.Triggers>
    </Style>
  </Window.Resources>
  <StackPanel>
    <Button>System default</Button>
    <Button Style="{StaticResource defaultButtonStyle}">My Default</Button>
    <Button Style="{StaticResource okButtonStyle}">Ok</Button>
    <Button Style="{StaticResource okButtonStyle}" IsEnabled="False">Ok disabled</Button>
  </StackPanel>
</Window>