【问题标题】:WPF MVVM changing property under controltemplate programmaticallyWPF MVVM以编程方式更改控件模板下的属性
【发布时间】:2020-02-05 21:55:50
【问题描述】:

我想以编程方式更改边框背景取决于 WeekType 属性。它具有 All、Even 和 Odd 属性。我的项目的颜色在 Border 的 Background 属性中。因此,当属性为 All 时,我希望它们保持 LightGray,但是当 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 类:

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

    }

我怎样才能做到这一点?

【问题讨论】:

    标签: wpf mvvm controltemplate


    【解决方案1】:

    您必须使用样式触发器。

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

    我希望它对你有用。

    【讨论】:

    • 谢谢!!你让我走上了正轨。您的回答几乎是正确的,我只需要删除 Border 的 Background 属性,因为它比 styl trigger setter 具有更高的优先级。干杯
    猜你喜欢
    • 1970-01-01
    • 2015-03-16
    • 2011-10-19
    • 2012-12-06
    • 2011-03-24
    • 2015-12-09
    • 1970-01-01
    • 2017-02-27
    • 2021-12-19
    相关资源
    最近更新 更多