powered by simpleCommunicator - 2.0.49     © 2025 Programmizd 02
Форумы / WPF, Silverlight [игнор отключен] [закрыт для гостей] / Не меняется цвет кнопки
6 сообщений из 6, страница 1 из 1
Не меняется цвет кнопки
    #39741353
asdfg1243
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Гость
Здравствуйте. Код:
Код: xml
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
<Window.Resources>
        <Style x:Key="button" TargetType="{x:Type ToggleButton}">
        <Style.Triggers>
                <Trigger Property="IsChecked" Value="true">
                <Setter Property="Background" Value="Red"></Setter>
                </Trigger>
        </Style.Triggers>
    </Style>
    </Window.Resources>
 
  <ToggleButton Content="but" Grid.Column="0" Grid.Row="0" Background="LightBlue" Style="{StaticResource button}" />



Что не так?
...
Рейтинг: 0 / 0
Не меняется цвет кнопки
    #39741361
Eld Hasp
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Гость
Из моих проб получилось только с переопределением дефолтного шаблона.
Более простой способ не смог найти.
...
Рейтинг: 0 / 0
Не меняется цвет кнопки
    #39741364
Eld Hasp
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Гость
В дефолтном шаблоне
Код: xml
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
49.
50.
51.
52.
53.
54.
55.
56.
57.
58.
59.
60.
61.
    <Window.Resources>
        <Style x:Key="FocusVisual">
            <Setter Property="Control.Template">
                <Setter.Value>
                    <ControlTemplate>
                        <Rectangle Margin="2" SnapsToDevicePixels="true" Stroke="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" StrokeThickness="1" StrokeDashArray="1 2"/>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
        <SolidColorBrush x:Key="Button.Checked.Background" Color="Red"/>
        <SolidColorBrush x:Key="Button.Static.Background" Color="LightBlue"/>
        <SolidColorBrush x:Key="Button.Static.Border" Color="#FF707070"/>
        <SolidColorBrush x:Key="Button.MouseOver.Background" Color="#FFBEE6FD"/>
        <SolidColorBrush x:Key="Button.MouseOver.Border" Color="#FF3C7FB1"/>
        <SolidColorBrush x:Key="Button.Pressed.Background" Color="#FFC4E5F6"/>
        <SolidColorBrush x:Key="Button.Pressed.Border" Color="#FF2C628B"/>
        <SolidColorBrush x:Key="Button.Disabled.Background" Color="#FFF4F4F4"/>
        <SolidColorBrush x:Key="Button.Disabled.Border" Color="#FFADB2B5"/>
        <SolidColorBrush x:Key="Button.Disabled.Foreground" Color="#FF838383"/>
        <Style x:Key="ToggleButtonStyle1" TargetType="{x:Type ToggleButton}">
            <Setter Property="FocusVisualStyle" Value="{StaticResource FocusVisual}"/>
            <Setter Property="Background" Value="{StaticResource Button.Static.Background}"/>
            <Setter Property="BorderBrush" Value="{StaticResource Button.Static.Border}"/>
            <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
            <Setter Property="BorderThickness" Value="1"/>
            <Setter Property="HorizontalContentAlignment" Value="Center"/>
            <Setter Property="VerticalContentAlignment" Value="Center"/>
            <Setter Property="Padding" Value="1"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type ToggleButton}">
                        <Border x:Name="border" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="true">
                            <ContentPresenter x:Name="contentPresenter" Focusable="False" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="Button.IsDefaulted" Value="true">
                                <Setter Property="BorderBrush" TargetName="border" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
                            </Trigger>
                            <Trigger Property="IsMouseOver" Value="true">
                                <Setter Property="Background" TargetName="border" Value="{StaticResource Button.MouseOver.Background}"/>
                                <Setter Property="BorderBrush" TargetName="border" Value="{StaticResource Button.MouseOver.Border}"/>
                            </Trigger>
                            <Trigger Property="IsPressed" Value="true">
                                <Setter Property="Background" TargetName="border" Value="{StaticResource Button.Pressed.Background}"/>
                                <Setter Property="BorderBrush" TargetName="border" Value="{StaticResource Button.Pressed.Border}"/>
                            </Trigger>
                            <Trigger Property="IsEnabled" Value="false">
                                <Setter Property="Background" TargetName="border" Value="{StaticResource Button.Disabled.Background}"/>
                                <Setter Property="BorderBrush" TargetName="border" Value="{StaticResource Button.Disabled.Border}"/>
                                <Setter Property="TextElement.Foreground" TargetName="contentPresenter" Value="{StaticResource Button.Disabled.Foreground}"/>
                            </Trigger>
                            <Trigger Property="IsChecked" Value="true">
                                <Setter Property="Background" TargetName="border" Value="{StaticResource Button.Checked.Background}"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Window.Resources>

Цвет вложенного Border зависит от ControlTemplate.Background только если не сработает не один из триггеров IsMouseOver, IsPressed, IsEnabled. Для пробы я определил после них триггер IsChecked - так работает.
...
Рейтинг: 0 / 0
Не меняется цвет кнопки
    #39741376
Фотография Shocker.Pro
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Участник
asdfg1243Что не так?у кнопки напрямую задан Background="LightBlue". Его нельзя переопределить триггером. Смотри иерархию приоритетов значений свойства.
Нужно примерно так
Код: xml
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
<Window.Resources>
    <Style x:Key="button" TargetType="{x:Type ToggleButton}">
        <Setter Property="Background" Value="LightBlue" />
        <Style.Triggers>
            <Trigger Property="IsChecked" Value="true">
                <Setter Property="Background" Value="Red"></Setter>
            </Trigger>
        </Style.Triggers>
    </Style>
</Window.Resources>
 
<ToggleButton Content="but" Grid.Column="0" Grid.Row="0" Style="{StaticResource button}" />
...
Рейтинг: 0 / 0
Не меняется цвет кнопки
    #39741379
Eld Hasp
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Гость
Shocker.Proasdfg1243Что не так?у кнопки напрямую задан Background="LightBlue". Его нельзя переопределить триггером. Смотри иерархию приоритетов значений свойства.
Нужно примерно так
Код: xml
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
<Window.Resources>
    <Style x:Key="button" TargetType="{x:Type ToggleButton}">
        <Setter Property="Background" Value="LightBlue" />
        <Style.Triggers>
            <Trigger Property="IsChecked" Value="true">
                <Setter Property="Background" Value="Red"></Setter>
            </Trigger>
        </Style.Triggers>
    </Style>
</Window.Resources>
 
<ToggleButton Content="but" Grid.Column="0" Grid.Row="0" Style="{StaticResource button}" />

Это была первая мысль - не вышло.
Поэтому и посмотрел дефолтный шаблон. Оказалось цвет фона в триггерах устанавливается напрямую у вложенного border, а не самого элемента. Как вариант можно убрать из триггеров в сеттерах Background TargetName="border" , тогда будет работать как Вы показали.
...
Рейтинг: 0 / 0
Не меняется цвет кнопки
    #39741407
asdfg1243
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Гость
Всем спасибо.
...
Рейтинг: 0 / 0
6 сообщений из 6, страница 1 из 1
Форумы / WPF, Silverlight [игнор отключен] [закрыт для гостей] / Не меняется цвет кнопки
Целевая тема:
Создать новую тему:
Автор:
Закрыть
Цитировать
Найденые пользователи ...
Разблокировать пользователей ...
Читали форум (0):
Пользователи онлайн (0):
x
x
Закрыть


Просмотр
0 / 0
Close
Debug Console [Select Text]