2014-05-14 3 views
0

몇 가지 속성 (잘 작동 함)이있는 사용자 지정 UserControl (MyControl)이 있습니다. UserControl을 사용하는 페이지가 일부 내용을 "붙여 넣기"하여 UserControl - ex에 직접 표시되도록하는 새 속성이 필요합니다. 길. 나는 노력했다. ContentPresenter에, 행운과 ContentControl을, StackPanel의 ...XAML UserControl의 FrameworkElement에 바인딩

MyControl.xaml

<ContentControl Grid.Column="0" Content="{Binding MyContent, ElementName=root}"></ContentControl> 

MyControl.xaml.cs

public object MyContent 
{ 
    get { return (object)GetValue(MyContentProperty); } 
    set { SetValue(MyContentProperty, value); } 
} 

public static readonly DependencyProperty MyContentProperty = 
    DependencyProperty.Register("MyContent", typeof(object), typeof(MyControl), new PropertyMetadata(null)); 

SomePage.xml

<mycontrols:MyControl x:Name="FavoritesButton"> 
    <mycontrols:MyControl.MyContent> 
    <Path Data="M1540.22,2082.07L1546.95,2102.78 1568.73,2102.78 1551.11,2115.58 1557.84,2136.29 1540.22,2123.49 1522.6,2136.29 1529.33,2115.58 1511.71,2102.78 1533.49,2102.78 1540.22,2082.07z" Stretch="Uniform" Fill="#FFFFFFFF" Width="50" Height="50" Margin="30"></Path> 
    </mycontrols:MyControl.MyContent> 
</mycontrols:MyControl> 

답변

0

내가 가진 그것은 작동했습니다 ... 해결책은 다음과 같습니다 :

MyControl.xaml

<ContentControl Content="{Binding Shape, ElementName=root}" /> 

MyControl.xaml.cs

public Shape Shape 
{ 
    get { return (Shape)GetValue(ShapeProperty); } 
    set { SetValue(ShapeProperty, value); } 
} 

public static readonly DependencyProperty ShapeProperty = 
     DependencyProperty.Register("Shape", typeof(Shape), typeof(MyControl), new PropertyMetadata(null)); 

SomePage.xml

<mycontrols:MyControl> 
    <mycontrols:MyControl.Shape> 
     <Path Data="M1540.22,2082.07L1546.95,2102.78 1568.73,2102.78 1551.11,2115.58 1557.84,2136.29 1540.22,2123.49 1522.6,2136.29 1529.33,2115.58 1511.71,2102.78 1533.49,2102.78 1540.22,2082.07z" Style="{StaticResource PathStyle}" /> 
    </mycontrols:MyControl.Shape> 
</mycontrols:MyControl> 
1

는 정말 잘 작동 다음이있다.

내 MainWindow.xaml을 (이 MVVM의 원칙에 대해 다소 있지만 ... 난 아직 동적으로 메인 윈도우의 단일 프레임 영역에서 내 사용자 컨트롤을 처리하기 위해 같은) :

<!-- Main Frame --> 
<Grid Grid.Column="1" Margin="10" Name="MainWindowFrameContent"> 
    <ItemsControl ItemsSource="{Binding Path=MainWindowFrameContent}" > 

     <!-- This controls the height automatically of the user control --> 
     <ItemsControl.ItemsPanel> 
     <ItemsPanelTemplate> 
      <UniformGrid Columns="1" IsItemsHost="True"/> 
     </ItemsPanelTemplate> 
     </ItemsControl.ItemsPanel> 
    </ItemsControl> 
</Grid> 

내 MainViewModel을 .cs :

using System; 
using System.Collections.ObjectModel; 
using System.Windows; 
using System.Windows.Input; 
using myProject.View; 
using myProject.Models; 

namespace myProject.ViewModel 
{ 
    public class MainViewModel : ObservableObject 
    { 
     public MainViewModel() { } 

     // This handles adding framework (UI) elements to the main window frame 
     ObservableCollection<FrameworkElement> _MainWindowFrameContent = new ObservableCollection<FrameworkElement>(); 
     public ObservableCollection<FrameworkElement> MainWindowFrameContent 
     { 
      get { return _MainWindowFrameContent; } 
      set { _MainWindowFrameContent = value; RaisePropertyChangedEvent("MainWindowFrameContent"); } 
     } 
    } 
} 

MainViewModel.cs는 "공용 클래스 MainViewModel : ObservableObject"입니다. 이를 통해 "RaisePropertyChangedEvent"를 구현할 수 있으므로 "MainWindowFrameContent"값을 변경하면 바인딩이 성공적으로 업데이트됩니다.

내 ObservableObject.cs : 다음

using System.ComponentModel; 

namespace myProject.ViewModel 
{ 
    public class ObservableObject : INotifyPropertyChanged 
    { 
     public event PropertyChangedEventHandler PropertyChanged; 

     protected void RaisePropertyChangedEvent(string propertyName) 
     { 
      var handler = PropertyChanged; 
      if (handler != null) 
       handler(this, new PropertyChangedEventArgs(propertyName)); 
     } 
    } 
} 

나는 MainWindowFrameContent에 항목을 추가 할 때, 나는 단순히 내 MainViewModel.cs 내에서 다음을 수행 : 그럼 당신은 만들 수 있습니다

void _AddNewUserControl() 
{ 
    myUserControl hControl = new myUserControl(); 
    MainWindowFrameContent.Clear(); // Clear the frame before displaying new content 
    MainWindowFrameContent.Add(hControl); 
} 

을 원하는대로 많은 사용자 컨트롤을 사용할 수 있습니다. 표시하려는 각 명령은 VM에 자체 void _AddNewUserControl 유형 메서드를 가질 수 있으며 기본 창에 표시됩니다. 다시 말하지만, MVVM 프레임 워크와는 조금 반대되는 점이 있지만 코드 기반에서이 점을 깨끗하게 유지합니다.

+0

내 MainWindow datacontext는 MainViewModel의 단일 인스턴스로 설정됩니다 (주 윈도우 생성자 내에서 완료 됨). –