WPF DependencyProperty 在 UserControl 中公开?

发布于 2024-10-02 14:40:09 字数 1189 浏览 0 评论 0原文

我有一个 UserControl,我想要一些自定义参数“radius”(double)和“contentSource”(string[])。

我的 UserControl 由一些嵌套控件组成:

<UserControl ...>
<Grid>
    <my:Menu ...>
        <my:Button>
        </my:Button>
        <my:Button>
        </my:Button>
        <my:Button>
        </my:Button>
    </my:Menu ...>
</Grid>

我试图通过以下方式公开参数:

    public double Rad
    {
        get { return (double)GetValue(RadProperty); }
        set { SetValue(RadProperty, value); }
    }
    public static readonly DependencyProperty RadProperty =
        DependencyProperty.Register(
            "Radius",
            typeof(double),
            typeof(Menu));

    public String[] DataSource
    {
        get { return (String[])GetValue(DataSourceProperty); }
        set { SetValue(DataSourceProperty, value); }
    }
    public static readonly DependencyProperty DataSourceProperty =
        DependencyProperty.Register(
            "DataSource",
            typeof(String[]),
            typeof(Menu));

但是,似乎有两个问题,“string[]”参数似乎导致崩溃,但大多数情况下,我根本无法设置“Radius”属性。我还需要做些什么来公开参数吗?

I have a UserControl where I'd like to have some parameters of customization "radius"(double) and "contentSource" (string[]).

My UserControl is composed of a few nested controls:

<UserControl ...>
<Grid>
    <my:Menu ...>
        <my:Button>
        </my:Button>
        <my:Button>
        </my:Button>
        <my:Button>
        </my:Button>
    </my:Menu ...>
</Grid>

I'm trying to expose the parameters with:

    public double Rad
    {
        get { return (double)GetValue(RadProperty); }
        set { SetValue(RadProperty, value); }
    }
    public static readonly DependencyProperty RadProperty =
        DependencyProperty.Register(
            "Radius",
            typeof(double),
            typeof(Menu));

    public String[] DataSource
    {
        get { return (String[])GetValue(DataSourceProperty); }
        set { SetValue(DataSourceProperty, value); }
    }
    public static readonly DependencyProperty DataSourceProperty =
        DependencyProperty.Register(
            "DataSource",
            typeof(String[]),
            typeof(Menu));

However, there seems to be two problems, the "string[]" parameter seems to be causing crashes, but mostly, I cannot set the "Radius" property at all. Is there something else I need to do to expose the parameter?

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

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

发布评论

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

评论(1

扭转时空 2024-10-09 14:40:09

您如何尝试访问这些值?我已将您的代码复制到用户控件中,它似乎工作正常。您是否为要从中访问这些值的对象设置了 DataContext?

这是我的测试代码,可能会有所帮助:

 public partial class uc : UserControl
{
    public uc()
    {
        InitializeComponent();

        this.DataContext = this;
        this.DataSource = new string[] { "hello","There" };
        this.Rad = 7;
    }
    public String[] DataSource
    {
        get { return (String[])GetValue(DataSourceProperty); }
        set { SetValue(DataSourceProperty, value); }
    }
    public static readonly DependencyProperty DataSourceProperty =
        DependencyProperty.Register(
            "DataSource",
            typeof(String[]),
            typeof(uc));

    public double Rad
    {
        get { return (double)GetValue(RadProperty); }
        set { SetValue(RadProperty, value); }
    }
    public static readonly DependencyProperty RadProperty =
        DependencyProperty.Register(
            "Radius",
            typeof(double),
            typeof(uc));

}

以及 XAML:

<UserControl x:Class="WpfApplication18.uc"
             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" 
             d:DesignHeight="300" d:DesignWidth="300">
    <Grid>
        <StackPanel>
            <TextBox Text="{Binding Path=DataSource[0]}"></TextBox>
            <TextBox Text="{Binding Path=DataSource[1]}"></TextBox>
            <TextBox Text="{Binding Path=Radius}"></TextBox>
            <TextBox Text="{Binding Path=Radius}"></TextBox>
        </StackPanel>
    </Grid>
</UserControl>

How are you trying to access the values? I have copied your code into a UserControl and it seems to work fine. Have you set the DataContext for the object you are accessing these values from?

Here is my test code, which might help:

 public partial class uc : UserControl
{
    public uc()
    {
        InitializeComponent();

        this.DataContext = this;
        this.DataSource = new string[] { "hello","There" };
        this.Rad = 7;
    }
    public String[] DataSource
    {
        get { return (String[])GetValue(DataSourceProperty); }
        set { SetValue(DataSourceProperty, value); }
    }
    public static readonly DependencyProperty DataSourceProperty =
        DependencyProperty.Register(
            "DataSource",
            typeof(String[]),
            typeof(uc));

    public double Rad
    {
        get { return (double)GetValue(RadProperty); }
        set { SetValue(RadProperty, value); }
    }
    public static readonly DependencyProperty RadProperty =
        DependencyProperty.Register(
            "Radius",
            typeof(double),
            typeof(uc));

}

and the XAML:

<UserControl x:Class="WpfApplication18.uc"
             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" 
             d:DesignHeight="300" d:DesignWidth="300">
    <Grid>
        <StackPanel>
            <TextBox Text="{Binding Path=DataSource[0]}"></TextBox>
            <TextBox Text="{Binding Path=DataSource[1]}"></TextBox>
            <TextBox Text="{Binding Path=Radius}"></TextBox>
            <TextBox Text="{Binding Path=Radius}"></TextBox>
        </StackPanel>
    </Grid>
</UserControl>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文