非常に柔軟なカスタムコントロールを作成しようとしています。私はExpanderContent
Dependency
プロパティにUserControl
をバインドできるように達成しようとしている柔軟性、スニペットの背後にあるコード:カスタムコントロールのコンテンツにUserControlをバインドする方法
public partial class ChartBar : UserControl
{
public UIElement ExpanderContent
{
get { return (UIElement)GetValue(ExpanderContentProperty); }
set { SetValue(ExpanderContentProperty, value); }
}
// Using a DependencyProperty as the backing store for ExpanderContent. This enables animation, styling, binding, etc...
public static readonly DependencyProperty ExpanderContentProperty =
DependencyProperty.Register("ExpanderContent", typeof(UIElement), typeof(ChartBar), new PropertyMetadata(null, OnExpanderContentChanged));
private static void OnExpanderContentChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
//throw new NotImplementedException();
}
.
.
.
私はXAMLでContentPresenter
を使用して試してみましたが、それは動作しません。私は明らかにボタンで埋めることができ、それは動作しますが、これはバインディングを介して動的コンテンツを破ります。さらに混乱何
<Expander x:Name="expander" Header="" VerticalAlignment="Top" d:LayoutOverrides="Width" Style="{DynamicResource ExpanderStyle1}">
<ContentPresenter Content="{Binding ExpanderContent, ElementName=TestControlWithContent}" />
<!--<WrapPanel HorizontalAlignment="Center" >
<Button Content="A" Style="{DynamicResource ButtonStyle1}" />
<Button Content="B" Style="{DynamicResource ButtonStyle1}" />
<Button Content="C" Style="{DynamicResource ButtonStyle1}" />
<Button Content="D" Style="{DynamicResource ButtonStyle1}" />
<Button Content="E" Style="{DynamicResource ButtonStyle1}" />
<Button Content="F" Style="{DynamicResource ButtonStyle1}" />
</WrapPanel>-->
</Expander>
は私が
// ChartBarParent is the name of the custom control set in XAML
ChartBarParent.Content = new TestControlWithContent();
を行うことができますし、それは同様にコールバックを発射するように動作します。
最終的には、UIElement
は依存関係プロパティであり、ContentPresenter
これを行うには正しい方法を使用していますか?
[ユーザーコントロールにコンテンツを追加する方法](http://stackoverflow.com/q/10427133/1997232)。 – Sinatr
私はそのリンクを見てきましたが、ソリューションはtypeof(オブジェクト)を使用していましたが、これは私が達成したいものではありません。私はUIElementまたはFrameworkElementを使いたいと思っていました。私は何かが欠けていますか(非常に可能です)? – Jason
'ContentPresenter'は' ControlTemplate'で使われることを意図しています。そのコンテンツを 'Content'プロパティをバインドするのではなく、その' ContentSource'プロパティをテンプレート化された親のプロパティの名前( 'ContentSource' 'はデフォルト値の' 'Content" 'を持っています。なぜなら、あなたはしばしば属性のないものをドロップすることができ、それは魔法のように何を意味するのですか?その特定の使用法が、あなたがやっていることには当てはまらない場合は、代わりに@ibebbsが提案して 'ContentControl'を使用してください。 –