Domanda

Ho un pulsante che sta guardando 2 comboboxes per assicurarsi che essi hanno un valore prima che venga attivato. Il problema è il modo in cui lo faccio sovrascrive lo stile di default dichiarato nel mio progetto a tema.

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

Ho provato ad aggiungere BasedOn = "{StaticResouce MyDefaultButtonStyleName}" al tag di stile, ma fa saltare in aria in fase di esecuzione.

L'errore è "valore 'System.Windows.Style' non può essere assegnato alla proprietà 'Style' dell'oggetto 'System.Windows.Controls.Button'. Può unica base su di uno stile con tipo di destinazione che è di tipo base 'IFrameworkInputElement '. Errore in oggetto 'System.Windows.Style' nel file di markup "

C'è un stato di fare questo in XAML senza sovrascrivere stile predefinito.

EDIT: Codice di esempio aggiornato. Ottengo un errore sul OKButtonStyle che dice "Impossibile aggiungere elementi alla proprietà 'Risorse', perché la proprietà può avere un solo elemento figlio se utilizza un tag collezione esplicito. Errore in oggetto 'System.Windows.Style' nel file di markup"

<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>
È stato utile?

Soluzione

Come è

BasedOn="{StaticResouce DefaultButton}"

suppone che si riferiscono allo stile pulsante di default? Questo si blocca a causa DefaultButton è una risorsa indefinita nella vostra app.

Dovrebbe essere:

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

EDIT:. Siamo spiacenti, ha risposto troppo frettolosamente

ho notato ora il pulsante ha uno stile = {} set, e punta ad uno stile chiamato OkBUttonStyle. Questo è lo stile che dovrebbe definire tutto ed essere basato sullo stile pulsante predefinito. Con tutto ciò, includo tali trigger. Quello che stai dicendo in XAML è che lo stile è il contenuto del tuo Button.

Forse po 'di codice vi aiuterà:

 <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>
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top