WPF MVVM 以编程方式在 controltemplate 下更改 属性

WPF MVVM changing property under controltemplate programmatically

我想以编程方式更改边框背景取决于 WeekType 属性。它有全部、偶数和奇数 属性。我的物品的颜色在 Border 的背景中 属性。因此,当 属性 是 All 时,我希望它们保持浅灰色,但是当 All 或 Even 时,我想更改此边框的颜色 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 class:

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);
        }

    }

我怎样才能做到这一点?

您必须使用样式触发器。

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

希望对你有用。