私は、スライスと塗りつぶしという2つの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>
オプションは、私が試した:私は明らかにユーザーコントロールでは動作しませんという名前のコンテンツを、許可したいので
は、私は、ユーザーコントロールを使用することはできません。私は彼らの バインディングのためのDataContextを使用することはできませんユーザーを意味し、自己にケーキコンテナのDataContextを設定しなければならないので、私は上記の例を好きではないHere.
を参照してください。
ケーキでは、どちらが正しい親であるかわからないため、RelativeSourceを使用してFillingプロパティをバインドすることはできません。 Here.
私はスライス要素、 で直接コンテンツプレゼンターを置き換えることができますが、それはテンプレートにあるので、私はテンプレート外スライスどこ へのアクセスを失う参照してください。私はvisualTreeをスライスにタイプキャストする可能性がありますが、これはメンテナンスの悪夢を感じさせます。
私は基本的に、それぞれのケーキは、スライスを持ちたい、ともそれをデフォルトの外観を与えながら
<Cake.Slice>
<DockPanel>
<Rectangle Background= “Blue”/>
<Rectangle Background= “Blue”/>
<Rectangle Background=“{Binding Filling}”/>
</DockPanel>
</Cake.Slice>
を使用して設定できるようにします。
EDIT: はどうやら私のスタイルは、ケーキプロジェクトとは対照的に、私はCake.dllを参照することを仕事に提供しません。それはなぜでしょうか?
あなたはケーキで何を達成したいのですか? –
@AyyappanSubramanianうん、ちょうど1つ追加されました。 – bwall