C# 设置用户控件';由xaml创建的自定义依赖关系项目

C# 设置用户控件';由xaml创建的自定义依赖关系项目,c#,wpf,xaml,user-controls,dependency-properties,C#,Wpf,Xaml,User Controls,Dependency Properties,下面是我的用户控件(MonthCal)的代码 public partial class MonthCal : UserControl { public DayOfWeek StartDayOfWeek { get { return (DayOfWeek)GetValue(StartDayOfWeekProperty); } set { SetValue(StartDayOfWeekProperty, value); } } public static readonly Depen

下面是我的用户控件(MonthCal)的代码

public partial class MonthCal : UserControl
{
    public DayOfWeek StartDayOfWeek { get { return (DayOfWeek)GetValue(StartDayOfWeekProperty); } set { SetValue(StartDayOfWeekProperty, value); } }
    public static readonly DependencyProperty StartDayOfWeekProperty = DependencyProperty.Register("StartDayOfWeek", typeof(DayOfWeek), typeof(MonthCellHeader), new UIPropertyMetadata(DayOfWeek.Sunday, StartDayOfWeek_PropertyChanged));
    //...
}
还有,这是一个xaml的MonthCal

<UserControl x:Class="GCDR.MonthCal"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d">
    <!-- ... -->
</UserControl>

那么,如何在xaml中设置“StartDayOfWeek”依赖属性呢?大家都知道,下面的代码是不可能的:

<UserControl ...
             StartDayOfWeek="Sunday">
</UserControl>


请给我一个帮助。

您不能在UserControl的标记中使用dependency属性,但您可以在将用户控件的实例放置在如下位置时使用它:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfApplication1">
    <Grid>
        <local:UserControl1 local:StartDayOfWeek="Friday" />
    </Grid>
</Window>

在用户控件中,可以将某些其他属性绑定到依赖项属性,如下所示:

<UserControl x:Class="WpfApplication1.UserControl1"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:WpfApplication1"
             mc:Ignorable="d" >
    <Grid>
        <Label Content="{Binding RelativeSource={RelativeSource AncestorType=local:UserControl1},Path=StartDayOfWeek}"  />
    </Grid>
</UserControl>

无法设置StartDayOfWeek的原因是XAML中的UserControl没有StartDayOfWeek依赖项属性,换句话说UserControl类型不是MonthCal类型

因为在XAML中,UserControl是UserControl1的基类,所以可以定义MonthCal继承的UserControl,然后在XAML中声明MonthCal

XAML

<local:MonthCal x:Class="WpfApplication1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:WpfApplication1" Title="MainWindow"
                Height="350" Width="525"
                StartDayOfWeek="Monday">
    <Grid></Grid>
</local:MonthCal>
namespace WpfApplication1
{
    public class MonthCal : Window
    {
        public DayOfWeek StartDayOfWeek { get { return (DayOfWeek)GetValue(StartDayOfWeekProperty); } set { SetValue(StartDayOfWeekProperty, value); } }
        public static readonly DependencyProperty StartDayOfWeekProperty = 
            DependencyProperty.Register("StartDayOfWeek", typeof(DayOfWeek), typeof(MonthCal), new UIPropertyMetadata(DayOfWeek.Sunday, StartDayOfWeek_PropertyChanged));

        private static void StartDayOfWeek_PropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
        }
    }

    public partial class MainWindow : MonthCal
    {
        public MainWindow()
        {
            InitializeComponent();
        }
    }
}