通过xaml设置用户控件的自定义依赖属性

发布于 2024-12-12 05:26:52 字数 1165 浏览 0 评论 0原文

这是我的用户控件(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));
    //...
}

另外,这是 MonthCal 的 xaml。

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

请给我帮助。

Here is my user control(MonthCal)'s code behind.

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

and also, here is a xaml of the 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>

And so, How can I set the 'StartDayOfWeek' dependency property in xaml? as you guys know, the following code is impossible:

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

Please give me a help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

等数载,海棠开 2024-12-19 05:26:52

您不能在 UserControl 的标记中使用依赖属性,但是当您将用户控件的实例放置在某个位置时可以使用它,如下所示:

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

You can not use the dependency property in markup of the UserControl but you can use it when you place instance of the user control somewhere like so:

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

With in your user control you can bind some other property to your dependency property like so:

<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>
葬花如无物 2024-12-19 05:26:52

为什么不能设置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();
        }
    }
}

Why you cannot set StartDayOfWeek is that UserControl in XAML does not have StartDayOfWeek dependency property, in other word UserControl type is not MonthCal type.

As, in XAML, UserControl is base class of UserControl1, you can define MonthCal inherited UserControl and then declare MonthCal in XAML.

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>

Codebehinde

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();
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文