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

发布于 2024-07-09 16:50:42 字数 5057 浏览 11 评论 0原文

我有一个按钮正在查看 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’的样式。错误在标记文件中的对象“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>

I have a Button that is looking at 2 comboboxes to make sure they have a value before it is enabled. The problem is the way I am doing it is overwriting the default style declared in my theme project.

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

I've tried adding BasedOn="{StaticResouce MyDefaultButtonStyleName}" to the style tag but it blows up at runtime.

The error is "'System.Windows.Style' value cannot be assigned to property 'Style' of object 'System.Windows.Controls.Button'. Can only base on a Style with target type that is base type 'IFrameworkInputElement'. Error at object 'System.Windows.Style' in markup file"

Is there a was to do this in XAML without overwriting default style.

EDIT: Code Sample Updated.
I get an error on OKButtonStyle saying "Cannot add element to property 'Resources', because the property can have only one child element if it uses an explicit collection tag. Error at object 'System.Windows.Style' in markup file "

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

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

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

发布评论

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

评论(1

最佳男配角 2024-07-16 16:50:42

这应该如何

BasedOn="{StaticResouce DefaultButton}"

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

应该是:

BasedOn="{StaticResource {x:Type Button}}"

编辑:抱歉,回答得太仓促了。

我注意到现在您的按钮有一个 Style={} 设置,并且指向一个名为 OkBUttonStyle 的样式。 这是应该定义所有内容并基于默认按钮样式的样式。 在所有事情中,我都包括了这些触发因素。 您在 XAML 中所说的是样式是按钮的内容。

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

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

How is this

BasedOn="{StaticResouce DefaultButton}"

supposed to refer to the default button style? This crashes because DefaultButton is an undefined resource in your app.

It should be:

BasedOn="{StaticResource {x:Type Button}}"

EDIT: Sorry, answered too hastily.

I noticed now your button has a Style={} set, and is pointing to a style called OkBUttonStyle. This is the style that should define everything and be based on the default button style. By everything, I include those triggers. What you are saying in the XAML is that Style is the Content of your Button.

Maybe some code will help:

 <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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文