2014-04-08 3 views
0

스타일을 종속 속성으로 표시하려고합니다. 기본적으로 중간에 사각형 표시기가 있으며 사용하는 컨트롤은 설정할 컨트롤을 포함하는 스타일 종속성 속성을 노출합니다. 그것은 컨트롤을 포함하는 사람들이 아이템에 대한 자신의 지식을 기반으로 채색 스타일을 제공 할 수있게합니다.스타일 속성 유형의 종속성 속성

<ItemsControl ItemsSource="{Binding Rows}"> 
    <ItemsControl.ItemTemplate> 
     <DataTemplate> 
      <StackPanel Orientation="Horizontal"> 
       <TextBlock Text="{Binding RowName}"/> 
       <ItemsControl ItemsSource="{Binding Statuses}"> 

        <Rectangle Style="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Grid}, Path=RectangleStyle}"/> 
       </ItemsControl> 
      </StackPanel> 
     </DataTemplate> 
    </ItemsControl.ItemTemplate> 
</ItemsControl> 

사용자 지정 컨트롤의 코드 숨김의 종속성 속성입니다.

<MyControl> 
     <MyControl.RectangleStyle> 
      <Style TargetType="{x:Type Rectangle}"> 
       <Setter Property="Fill" Value="Red"/> 
      </Style> 
     </MyControl.RectangleStyle> 
    </MyControl> 

이 전혀 작동하지 않는 나의 접근 방식도 맞는지 잘 모르겠어요 :

public Style RectangleStyle 
    { 
     get { return (Style)GetValue(RectangleStyleProperty); } 
     set { SetValue(RectangleStyleProperty, value); } 
    } 

    public static readonly DependencyProperty RectangleStyleProperty = 
     DependencyProperty.Register("RectangleStyle", typeof(Style), typeof(MyControl), new PropertyMetadata(null)); 

은 다음과 같이 사용됩니다.

답변

0

바보 같은 구문 오류입니다. 내부 ItemsControl에 ItemsTemplate을 설정해야했습니다. ItemsControl의 내용을 Rectangle로 설정했습니다.

<ItemsControl ItemsSource="{Binding Rows}"> 
    <ItemsControl.ItemTemplate> 
     <DataTemplate> 
      <StackPanel Orientation="Horizontal"> 
       <TextBlock Text="{Binding RowName}"/> 
       <ItemsControl ItemsSource="{Binding Statuses}"> 
        <ItemsControl.ItemsPanel> 
         <ItemsPanelTemplate> 
          <StackPanel Orientation="Horizontal"/> 
         </ItemsPanelTemplate> 
        </ItemsControl.ItemsPanel> 
        <ItemsControl.ItemTemplate> 
         <DataTemplate> 
          <Rectangle Style="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Grid}, Path=RectangleStyle}"/> 
         </DataTemplate> 
        </ItemsControl.ItemTemplate> 
       </ItemsControl> 
      </StackPanel> 
     </DataTemplate> 
    </ItemsControl.ItemTemplate> 
</ItemsControl>