Вопрос о ресурсах WPF / Styles & Scope - PullRequest
0 голосов
/ 03 сентября 2010

Я только начинаю WPF и удивляюсь, почему это работает

<Window ...>
    <Window.Resources>
        <Style x:Name="buttonStyle">
            <Style.Setters>
                <Setter Property="Button.FontWeight" Value="Bold" />
                <Setter Property="Button.Foreground" Value="Aqua" />
            </Style.Setters>
        </Style>
    </Window.Resources>
    <Grid>
        <StackPanel>
            <Button ... Style="{StaticResource buttonStyle}" />
        </StackPanel>
    </Grid>
</Window>

но это не удалось,

<Window ...>
    <Window.Resources>
        <Style x:Name="buttonStyle">
            <Style.Setters>
                <Setter Property="Button.FontWeight" Value="Bold" />
                <Setter Property="Button.Foreground" Value="Aqua" />
            </Style.Setters>
        </Style>
    </Window.Resources>
    <Grid>
        <StackPanel ...>
            <StackPanel.Resources>
                <Style x:Name="buttonStyle2">
                    <Setter Property="Button.Foreground" Value="Red" />
                </Style>
            </StackPanel.Resources>
            <Button ... Style="{StaticResource buttonStyle}" />
            <Button ... Style="{StaticResource buttonStyle2}" />
        </StackPanel>
    </Grid>
</Window>

ошибка

  • Не удалось разрешить ресурс "buttonStyle2"
  • Не удалось разрешить ресурс "buttonStyle"

buttonStyle разрешается нормально в 1-м коде (без 2-й кнопки)

1 Ответ

0 голосов
/ 03 сентября 2010

обр ... мой плохой, я должен использовать x:Key не x:Name

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...