WPF MVVM изменяет свойство под управлением шаблона программно - PullRequest
0 голосов
/ 06 февраля 2020

Я хочу изменить границы фона программно зависит от свойства WeekType. Он имеет все, четные и нечетные свойства. Цвет моих предметов находится в свойстве Border's Background. Поэтому, когда для свойства установлено значение «Все», я хочу, чтобы они оставались LightGray, а когда «Все» или «Даже», я хочу изменить цвет этой границы. XAML:

<Style TargetType="{x:Type summary:SummaryHourUnitItem}">
        <Setter Property="StartTime" Value="{Binding StartTime}" />
        <Setter Property="EndTime" Value="{Binding EndTime}" />
        <Setter Property="WeekType" Value="{Binding WeekType}" />
        <Setter Property="SubGroup" Value="{Binding SubGroup}" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type summary:SummaryHourUnitItem}">
                    <Border Width="Auto" BorderThickness="1,1,1,1" BorderBrush="Black" Background="LightGray"
                            Margin="0" Padding="3,1.5,0,1.5" >

                        <ContentPresenter>
                            <ContentPresenter.Resources>
                                <Style TargetType="{x:Type TextBlock}">
                                    <Setter Property="FontSize" Value="9" />
                                    <Setter Property="Foreground" Value="Black" />
                                </Style>
                            </ContentPresenter.Resources>
                        </ContentPresenter>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

Класс SummaryHourUnitItem:

public class SummaryHourUnitItem : ButtonBase
    {
        public static readonly DependencyProperty StartTimeProperty =
            SummaryTimeSlotPanel.StartTimeProperty.AddOwner(typeof(SummaryHourUnitItem));

        public static readonly DependencyProperty EndTimeProperty =
            SummaryTimeSlotPanel.EndTimeProperty.AddOwner(typeof(SummaryHourUnitItem));

        public static readonly DependencyProperty WeekTypeProperty =
            SummaryTimeSlotPanel.WeekTypeProperty.AddOwner(typeof(SummaryHourUnitItem));

        public static readonly DependencyProperty SubGroupProperty =
            SummaryTimeSlotPanel.SubGroupProperty.AddOwner(typeof(SummaryHourUnitItem));

        static SummaryHourUnitItem()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(SummaryHourUnitItem),
                new FrameworkPropertyMetadata(typeof(SummaryHourUnitItem)));
        }

        public bool StartTime
        {
            get => (bool) GetValue(StartTimeProperty);
            set => SetValue(StartTimeProperty, value);
        }

        public bool EndTime
        {
            get => (bool) GetValue(EndTimeProperty);
            set => SetValue(EndTimeProperty, value);
        }

        public WeekType WeekType
        {
            get => (WeekType) GetValue(WeekTypeProperty);
            set
            {
                SetValue(WeekTypeProperty, value);
            }
        }

        public SubGroup SubGroup
        {
            get => (SubGroup)GetValue(SubGroupProperty);
            set => SetValue(SubGroupProperty, value);
        }

    }

Как можно Я достиг этого?

1 Ответ

0 голосов
/ 06 февраля 2020

Вы должны использовать триггер стиля.

<Border Width="Auto" BorderThickness="1,1,1,1" BorderBrush="Black" Background="LightGray"
                            Margin="0" Padding="3,1.5,0,1.5" >
    <Border.Style>
        <Style BasedOn="{StaticResource {x:Type Border}}" TargetType="{x:Type Border}">
            <Setter Property="Background" Value="LightGray" />
            <Style.Triggers>
                <DataTrigger Binding="{Binding WeekType}" Value="XXXX">
                    <Setter Property="Background" Value="Red" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </TextBox.Style>    
    <ContentPresenter>
        <ContentPresenter.Resources>
            <Style TargetType="{x:Type TextBlock}">
                <Setter Property="FontSize" Value="9" />
                <Setter Property="Foreground" Value="Black" />
            </Style>
        </ContentPresenter.Resources>
    </ContentPresenter>
</Border>

Надеюсь, он работает для вас.

...