2014-03-05 3 views
2
나는이에 대한 몇 가지 기사를 읽고 난 메신저 누군가가 도움을 줄 수 잘못 여기서 뭘 볼 수 없습니다

:DependencyProperty에 실버 라이트 UserControl을

나는 CreateRuleItemView라는 UserControl을 내가 여기에 종속성 속성을 추가 할 수 있습니다 내 ViewModel도 바인딩 할 수 있습니다. 지금까지 나는 가지고있다. 내가 다음 사용자의 컨트롤 XAML을 속성에 액세스하려고하면

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) 
    { 
    } 

    } 

} 

내가 얻을 : 멤버 "ShowEditTablePopup"

<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가 인식되지 않거나 액세스 할 수 없습니다입니다.

오류 3 속성 'ShowEditTablePopup은'유형 'UserControl을'

오류 2 속성 'ShowEditTablePopup'유형에서 찾을 수 없습니다에 존재하지 않는 'UserControl을'.

편집 1 : Ok 내 기본 설정 창에서 코드를 바인딩하여이 문제를 해결할 수 있습니다.

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

답변

0

당신이 이것을 달성 할 수 없을 것입니다 UserControl (난 그냥 로컬 코드를 다시 할 때 <local:CreateRuleItemView와 XAML에서 <UserControl... 부분 선언을 교체하려고했지만,이 순환 참조를 초래하므로 원 컴파일하지 않으면 잠재적으로 XamlParseException이됩니다). 나는 (내가 WPF와 함께 이런 짓을 너무 네임 스페이스가 다를 수 있습니다 그렇지 않으면 코드가 작동합니다) 당신이 속성을 추가하고 대신을 템플릿을 수있는 ContentControl에서 상속 컨트롤을 써서 :

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

안녕하세요. 나는 내 View를 할당 할 때 Code 내 Binding을 추가하기 위해이 문제를 해결할 수 있었다. –

+0

차가워지면 어떻게되는지 알려주세요. –