2017-11-17 1 views
1

나는 슬라이스 및 필이라는 두 개의 DependencyProperties를 포함하는 사용자 정의 컨트롤 인 Cake를 가지고있다. 슬라이스에 액세스 할 수있는 스타일을 어떻게 만들 수 있습니까? 슬라이스를 디자인 할 수 있습니까?내 케이크 먹고 그것을 먹기 : 컨테이너 및 그것의 내용 바인딩

<Style TargetType={x:Type local:Cake}> 
    //I don't like setting DataContext Here 
    <Setter Property="DataContext" Value="{Binding RelativeSource={RelativeSource Self}}/> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType={x:Type local:Cake}> 
       <Grid DataContext="{Binding RelativeSource={RelativeSource TemplatedParent}}">       

        //This is how I display a slice 
        <ContentPresenter Content={Binding Slice}/> 

        //This is how cake decorations are displayed 
        <ItemsPresenter/> 

       </Grid> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
    <Setter Property="Slice"> 
     <Setter.Value> 

      //Design Slice Here - it's easy to override when I want 
      <Slice Filling={Binding Filling}> // it's just in a setter. 

     </Setter.Value> 
    </Setter> 
    <Setter Property="DataContext" Value="{Binding RelativeSource={RelativeSource Self}}/> 
</Style> 

옵션이 나는 시도했다 : 나는 분명히 사용자 컨트롤 작동하지 않습니다라는 내용을 허용 할 때문에

  • 은 내가 UserControl을 사용할 수 없습니다. 자신의 바인딩에 대한 DataContext를 사용할 수 없습니다 사용자를 의미, 내가 자기에게 케이크 컨테이너의 DataContext를 설정해야하기 때문에 위의 예를 싫어 Here.

  • 를 참조하십시오.

  • 케이크를 사용하면 스타일이 올바른 부모인지 알 수 없으므로 RelativeSource를 사용하여 필링 속성을 바인딩 할 수 없습니다. Here.

  • 내가 슬라이스 요소, 직접 컨텐츠 발표자를 대체 할 수 있지만, 템플릿에 있기 때문에, 내가 템플릿 외부 슬라이스 어디서나 에 대한 액세스를 풀어 참조하십시오. visualTree를 슬라이스까지 내림차순으로 캐스팅 할 수는 있지만 유지 관리의 악몽을 느끼게됩니다.

    Access Slice from Cake Directly, but be able to edit it in xaml

나는 기본적으로 각 케이크 한 조각을 가지고, 또한 그것을 기본 모양을 제공하면서

<Cake.Slice> 
    <DockPanel> 
     <Rectangle Background= “Blue”/> 
     <Rectangle Background= “Blue”/> 
     <Rectangle Background=“{Binding Filling}”/> 
    </DockPanel> 
</Cake.Slice> 

사용하여 설정할 수 있어야합니다.

편집 : 은 분명히 내 스타일이 작품의 케이크 프로젝트 반대로 나는 Cake.dll를 참조하는 것을 제공 않습니다. 왜 그랬을까요?

+0

케이크에서 얻고 싶은 사진이 있습니까? –

+0

@AyyappanSubramanian 예, 방금 추가했습니다. – bwall

답변

1

이것은 꼭 필요한 것은 아니지만,이를 달성하는 방법을 알려줄 수 있기를 바랍니다.

첫째, 당신은 {Binding Slice, RelativeSource={RelativeSource TemplatedParent}}에 대한 바로 가기가 (그래서 당신이 할 수있는 단지이다, 당신은 {TemplateBinding Slice}를 사용하여 케이크의의 ControlTemplate에서 (작성 및 조각) 케이크 컨트롤의 속성에 바인딩 할 수 있습니다, 컨트롤 자체에 DataContext를 설정할 필요가 없습니다 하나 또는 다른 것을 사용하십시오).

ControlTemplate의 ItemPresenter가 제시해야하는 항목이나 Slice 및 Filling 속성 유형이 무엇인지 알지 못하기 때문에이 컨트롤은 단순화 된 버전입니다. 이 예에서는 채우기가 SolidColorBrush이고 슬라이스가 Style입니다. 이 스타일은 Cake의 ControlTemplate 안에있는 ContentControl에 적용되므로 미리 정의 된 스타일을 슬라이스에 적용하고 원하는 채우기를 적용 할 수 있습니다 (Slice 속성의 목적이 다른 경우 SliceStyle이라는 또 다른 속성을 도입 할 수 있습니다).

케이크 제어 :

public class Cake : Control 
{ 
    static Cake() 
    { 
     DefaultStyleKeyProperty.OverrideMetadata(
      typeof(Cake), 
      new FrameworkPropertyMetadata(typeof(Cake))); 
    } 

    public SolidColorBrush Filling 
    { 
     get { return (SolidColorBrush)GetValue(FillingProperty); } 
     set { SetValue(FillingProperty, value); } 
    } 

    public static readonly DependencyProperty FillingProperty = 
     DependencyProperty.Register(
      "Filling", 
      typeof(SolidColorBrush), 
      typeof(Cake), 
      new PropertyMetadata(Brushes.Transparent)); 

    public Style Slice 
    { 
     get { return (Style)GetValue(SliceProperty); } 
     set { SetValue(SliceProperty, value); } 
    } 

    public static readonly DependencyProperty SliceProperty = 
     DependencyProperty.Register(
      "Slice", 
      typeof(Style), 
      typeof(Cake), 
      new PropertyMetadata(null)); 
} 

기본 스타일 (generic.xaml을) :

<Style TargetType="{x:Type local:Cake}"> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="{x:Type local:Cake}"> 
       <ContentControl Width="{TemplateBinding Width}" 
           Height="{TemplateBinding Height}" 
           Style="{TemplateBinding Slice}"/> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 

다른 슬라이스 스타일 (이러한 ContentControl을 위해의 ControlTemplate을 설정하는, 그래서 당신이 문제 번호 3 명중하지 않을 것이다 질문) :

<Window.Resources> 
    <Style x:Key="TwoLayeredSlice" TargetType="{x:Type ContentControl}"> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate> 
        <Grid> 
         <Grid.RowDefinitions> 
          <RowDefinition Height="*"/> 
          <RowDefinition Height="*"/> 
         </Grid.RowDefinitions> 
         <Rectangle Fill="{Binding Filling, 
          RelativeSource={RelativeSource AncestorType={x:Type local:Cake}}}"/> 
         <Rectangle Fill="Brown" 
            Grid.Row="1"/> 
        </Grid> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 

    <Style x:Key="FourLayeredSlice" TargetType="{x:Type ContentControl}"> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate> 
        <Grid> 
         <Grid.RowDefinitions> 
          <RowDefinition Height="*"/> 
          <RowDefinition Height="*"/> 
          <RowDefinition Height="*"/> 
          <RowDefinition Height="*"/> 
         </Grid.RowDefinitions> 
         <Rectangle Fill="{Binding Filling, 
          RelativeSource={RelativeSource AncestorType={x:Type local:Cake}}}"/> 
         <Rectangle Fill="Brown" 
            Grid.Row="1"/> 
         <Rectangle Fill="{Binding Filling, 
          RelativeSource={RelativeSource AncestorType={x:Type local:Cake}}}" 
            Grid.Row="2"/> 
         <Rectangle Fill="Brown" 
            Grid.Row="3"/> 
        </Grid> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 
</Window.Resources> 

그리고 사용 제어 :

<Grid Background="Gray"> 
    <local:Cake Width="200" 
       Height="100" 
       HorizontalAlignment="Left" 
       Filling="Gold" 
       Slice="{StaticResource TwoLayeredSlice}"/> 
    <local:Cake Width="200" 
       Height="100" 
       HorizontalAlignment="Center" 
       Filling="Pink" 
       Slice="{StaticResource FourLayeredSlice}"/> 
    <local:Cake Width="200" 
       Height="100" 
       HorizontalAlignment="Right" 
       Filling="Blue" 
       Slice="{StaticResource FourLayeredSlice}"/> 
</Grid> 

Cakes

도요 위하여!

+0

여전히 슬라이스에 직접 액세스 할 수있는 것은 아니지만, RelativeSource를 사용하는 방법을 찾은 덕분에 환상적인 ** 디자인 작업을 수행 할 수 있습니다! – bwall

관련 문제