【问题标题】:DependencyProperty Silverlight UserControlDependencyProperty Silverlight 用户控件
【发布时间】:2014-04-07 21:27:22
【问题描述】:

我已经阅读了几篇关于此的文章,但我看不出我在这里做错了什么,谁能帮忙:)

我有一个名为 CreateRuleItemView 的 UserControl 我想在这里添加一个依赖属性,我也可以绑定我的 ViewModel。到目前为止我有。

public partial class CreateRuleItemView : UserControl
{
    public CreateRuleItemView()
    {
        InitializeComponent();
    }

    public Boolean ShowEditTablePopup
    {
        get
        {

            return (Boolean)this.GetValue(ShowEditTablePopupProperty);
        }
        set
        {

            this.SetValue(ShowEditTablePopupProperty, value);
        }
    }

    public static readonly DependencyProperty ShowEditTablePopupProperty = DependencyProperty.Register("ShowEditTablePopup", typeof(Boolean), typeof(CreateRuleItemView), new PropertyMetadata(null, OnShowEditTablePopupChanged));

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

    }

}

如果我尝试访问用户控件 Xaml 中的属性,我会得到:

<UserControl x:Class="Views.Setup.CreateRuleItemView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    d:DataContext="{d:DesignInstance Type=vm:CreateRuleItemViewModel, IsDesignTimeCreatable=False}"         
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400" ShowEditTablePopup="{Binding DataContext.ShowEditTablePopup}" >

错误 1 ​​成员“ShowEditTablePopup”无法识别或无法访问。

错误 3 类型“UserControl”上不存在属性“ShowEditTablePopup”

错误 2 在类型“UserControl”中找不到属性“ShowEditTablePopup”。

编辑 1: 好的,通过在我设置视图的主窗口后面的代码中添加绑定来解决这个问题。

Setup.CreateRuleItemView v = new Setup.CreateRuleItemView();  
BindingOperations.SetBinding(v, CreateRuleItemView.EditTablePopupProperty, new Binding("EditTablePopup"));

【问题讨论】:

    标签: c# silverlight user-controls dependency-properties


    【解决方案1】:

    您将无法使用 UserControl 实现此目的(我刚刚尝试在本地重新创建代码时将 XAML 中的 &lt;UserControl... 部分声明替换为 &lt;local:CreateRuleItemView,但这会导致循环引用和因此不会编译/可能会导致XamlParseException)。我会编写一个继承自 ContentControl 的控件,您可以向其中添加属性并改为模板(我使用 WPF 执行此操作,因此名称空间可能不同,否则代码将起作用):

    using System;
    using System.Windows;
    using System.Windows.Controls;
    
    namespace DepPropTest
    {
        /// <summary>
        /// Description of CreateRuleItemView.
        /// </summary>
        public class CreateRuleItemView : ContentControl
        {
            public CreateRuleItemView()
            {
            }
    
            public static readonly DependencyProperty ShowEditTablePopupProperty = 
                DependencyProperty.Register("ShowEditTablePopup", typeof (bool), typeof (CreateRuleItemView), new PropertyMetadata());
    
            public bool ShowEditTablePopup
            {
                get { return (bool) GetValue(ShowEditTablePopupProperty); }
                set { SetValue(ShowEditTablePopupProperty, value); }
            }
    
        }
    }
    

    那么你可以如下使用它(这个例子顺便使用了WPF,因此Window是父控件):

    <Window x:Class="DepPropTest.Window1"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:local="clr-namespace:DepPropTest"
        Title="DepPropTest" Height="300" Width="300">
    
        <local:CreateRuleItemView Width="300"
                                  Height="300"
                                  ShowEditTablePopup="True">
            <local:CreateRuleItemView.Template>
                <ControlTemplate>
                    <!-- define your control's visual appearance... -->
                </ControlTemplate>
            </local:CreateRuleItemView.Template>
    
            <TextBox Text="Some content for your view" />
        </local:CreateRuleItemView>
    </Window>
    

    【讨论】:

    • 您好,谢谢您,我将对此进行调查。我已经设法解决了这个问题,现在我在分配视图时添加了代码中的绑定。
    • 酷,让我知道你过得怎么样。
    猜你喜欢
    • 2012-02-08
    • 2019-09-18
    • 2023-03-30
    • 2015-01-15
    • 1970-01-01
    • 2010-10-31
    • 2010-12-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多