如何从主 window 设置自定义控件的 属性?

How to set a property of a custom control from the main window?

我有一个自定义控件,这是generic.axml代码:

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:Calendario"
    xmlns:MyNamespace="clr-namespace:Calendario;assembly=Calendario"
    xmlns:Converters="clr-namespace:Calendario.Converters">

    <Converters:DateConverter x:Key="DateConverter"></Converters:DateConverter>
    <Converters:DayBorderColorConverter x:Key="DayBorderColorConverter"></Converters:DayBorderColorConverter>

    <Style TargetType="{x:Type local:CalendarioPersonalizado}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:CalendarioPersonalizado}">
                    <Border Background="{TemplateBinding Background}"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}">
                        <DockPanel>
                            <TextBlock Text="{Binding Date}" />

                            <Grid Height="30" DockPanel.Dock="Top">

                            </Grid>


                            <ItemsControl ItemsSource="{Binding Days}">
                                <ItemsControl.ItemTemplate>
                                    <DataTemplate>
                                        <Border BorderBrush="{Binding ColorRecuadroExterno, Mode=TwoWay}" BorderThickness="1" Padding="0">                                         
                                        </Border>
                                    </DataTemplate>
                                </ItemsControl.ItemTemplate>
                            </ItemsControl>
                        </DockPanel>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>

我的 Calendario.cs 具有依赖关系 属性:

public static readonly DependencyProperty ColorRecuadroExternoProperty = DependencyProperty.Register("ColorRecuadroExterno", typeof(Brush), typeof(CalendarioPersonalizado));
public Brush ColorRecuadroExterno
{
    get { return (Brush)GetValue(ColorRecuadroExternoProperty); }
    set { SetValue(ColorRecuadroExternoProperty, value); }
}

后来在我的主要windows中我使用了控件:

<local:CalendarioPersonalizado x:Name="cCalendario" ColorRecuadroExterno="Green"/>

问题是日历中日期的边框没有设置为绿色,就像我在主 window 中尝试设置的那样。

我也在隐藏的代码中试过这个:

cCalendario.ColorRecuadroExterno = System.Windows.Media.Brushes.Green;

但是没有设置颜色

我想做的是在我的主应用程序中设置自定义控件中的边框颜色。

谢谢。

您的 DependencyProperty 类型为 Brush。您不能将 "Green" 隐式转换为 Brush。

您需要使用转换器将颜色的字符串表示形式转换为画笔,或者使您的 属性 类型颜色,并将其绑定到适当的 属性.

如果您在 local:CalendarioPersonalizado class 中放置一个回调方法,并在此回调方法中设置您的背景。我认为它会起作用。

public static readonly DependencyProperty ColorRecuadroExternoProperty = DependencyProperty.Register("ColorRecuadroExterno", typeof(Brush), typeof(CalendarioPersonalizado),
    new PropertyMetadata(Brushes.Brown, Callback));

private static void Callback(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
    CalendarioPersonalizado obj = d as CalendarioPersonalizado;
    obj.ColorRecuadroExterno.Background = (Brush)e.NewValue;
}