2013-08-09 2 views
1

내가 콤보처럼 (DataContext를 바인딩하지 않고) 직접 XAML로 항목의 컬렉션을 받아들이는 사용자 정의 컨트롤을 만들려고 해요 : 내 컨트롤의 클래스에서,항목이 xaml에 정의 된 UC?

<ComboBox> 
     <ComboBoxItem Content="test"/> 
     <ComboBoxItem Content="test2"/> 
     <ComboBoxItem Content="test3"/> 
    </ComboBox> 

또한, 내가 원하는 XAML에서 UC 내에 입력 된 항목 목록을받습니다. 이것이 가능한가?

** 편집 **

내가 여러 목록 말하자면, 세 가지 목록을받을 필요

:

<MyControl> 
     <MyControl.FirstCollection> 
      <MyControlItem Content="test"/> 
      <MyControlItem Content="test2"/> 
      <MyControlItem Content="test3"/> 
     </MyControl.FirstCollection> 
     <MyControl.SecondCollection> 
      <MyControlItem Content="test4"/> 
      <MyControlItem Content="test5"/> 
      <MyControlItem Content="test6"/> 
     </MyControl.SecondCollection> 
     <MyControl.LastCollection> 
      <MyControlItem Content="test7"/> 
      <MyControlItem Content="test8"/> 
      <MyControlItem Content="test9"/> 
     </MyControl.LastCollection> 
    </MyControl> 

가 어떻게이 세 가지 목록을받을 수 있습니까?

+0

중복 (http://stackoverflow.com/questions/9094486/adding-children-to-usercontrol). 나는 받아 들인 대답에서 해결책을 사용하고있다 완전하게 작동한다. –

+0

좋은 해결책이지만 컨테이너를 사용하여 항목을 보이게하고 싶지는 않지만 Children 컨트롤을 가져 와서 List 또는 Collection에 넣기를 원합니다. – Guilherme

+0

이전 의견을 이해할 수 있는지 잘 모르겠습니다. "용기 사용"이란 무엇을 의미합니까? –

답변

1
나는 눈에 보이는 내 아이템을 만들기 위해 컨테이너를 사용하지 않으려는

, 나는 단지 어린이 컨트롤을 얻을 그들은 시각적 요소가없는 경우 목록 또는 컬렉션

에 넣어 원하는 당신은 사용자 정의 컨트롤에

예를 목록을 만들 수 있습니다

UserControl을 (단지 FrameworkElement 목록 사용) :

,691을
public partial class UserControl1 : UserControl 
{ 
    public UserControl1() 
    { 
     InitializeComponent(); 
    } 

    public ObservableCollection<FrameworkElement> FirstCollection 
    { 
     get { return (ObservableCollection<FrameworkElement>)GetValue(FirstCollectionProperty); } 
     set { SetValue(FirstCollectionProperty, value); } 
    } 

    // Using a DependencyProperty as the backing store for FirstCollection. This enables animation, styling, binding, etc... 
    public static readonly DependencyProperty FirstCollectionProperty = 
     DependencyProperty.Register("FirstCollection", typeof(ObservableCollection<FrameworkElement>), typeof(UserControl1), new PropertyMetadata(new ObservableCollection<FrameworkElement>())); 

    public ObservableCollection<FrameworkElement> SecondCollection 
    { 
     get { return (ObservableCollection<FrameworkElement>)GetValue(SecondCollectionProperty); } 
     set { SetValue(SecondCollectionProperty, value); } 
    } 

    // Using a DependencyProperty as the backing store for SecondCollection. This enables animation, styling, binding, etc... 
    public static readonly DependencyProperty SecondCollectionProperty = 
     DependencyProperty.Register("SecondCollection", typeof(ObservableCollection<FrameworkElement>), typeof(UserControl1), new PropertyMetadata(new ObservableCollection<FrameworkElement>())); 

    public ObservableCollection<FrameworkElement> LastCollection 
    { 
     get { return (ObservableCollection<FrameworkElement>)GetValue(LastCollectionProperty); } 
     set { SetValue(LastCollectionProperty, value); } 
    } 

    // Using a DependencyProperty as the backing store for LastCollection. This enables animation, styling, binding, etc... 
    public static readonly DependencyProperty LastCollectionProperty = 
     DependencyProperty.Register("LastCollection", typeof(ObservableCollection<FrameworkElement>), typeof(UserControl1), new PropertyMetadata(new ObservableCollection<FrameworkElement>())); 

} 

사용법 : [UserControl을에 아이를 추가]의

<Window x:Class="WpfApplication8.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:local="clr-namespace:WpfApplication8" 
     Title="MainWindow" Height="350" Width="525" Name="UI"> 
    <Grid> 
     <local:UserControl1 > 
      <local:UserControl1.FirstCollection> 
       <Label /> 
      </local:UserControl1.FirstCollection> 
      <local:UserControl1.SecondCollection> 
       <Label /> 
      </local:UserControl1.SecondCollection> 
      <local:UserControl1.LastCollection> 
       <Label /> 
      </local:UserControl1.LastCollection> 
     </local:UserControl1> 
    </Grid> 
</Window> 
+0

와우, 위대한, 이것은 내가 원하는만큼 정확하게 작동합니다. 정말 고맙습니다. – Guilherme

관련 문제