【问题标题】:WPF DependencyProperty Exposing in UserControl?WPF DependencyProperty 在 UserControl 中公开?
【发布时间】:2010-11-15 09:56:30
【问题描述】:

我有一个 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”属性。我还需要做些什么来公开参数吗?

【问题讨论】:

    标签: wpf dependency-properties argument-passing


    【解决方案1】:

    您如何尝试访问这些值?我已将您的代码复制到 UserControl 中,它似乎工作正常。您是否为要从中访问这些值的对象设置了 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>
    

    【讨论】:

    • 这看起来不错,有一些我不知道的东西,但是我在不同的类中使用我的 UserControl,并执行以下操作: 我可以类似的东西?
    • 是的,但是您需要使用公共属性的名称,即“Rad”。这将起作用:
    • 啊,我现在发现我的问题是当我们在 CustomControl 的构造函数中时尚未设置值。当我可以读取已设置的值时,是否存在尚未显示控件的地方?
    • 控件被实例化,然后它的属性被分配,所以你的构造函数不适合任何依赖于属性值的东西。试试 Loaded 事件(你可以在你的构造函数中钩住它,但是延迟你的逻辑直到事件触发)。
    猜你喜欢
    • 2012-04-18
    • 1970-01-01
    • 2010-09-23
    • 1970-01-01
    • 1970-01-01
    • 2010-12-19
    • 1970-01-01
    • 2012-03-02
    • 2013-01-20
    相关资源
    最近更新 更多