2011-01-08 2 views
0

私はかなり新しいWPFです。ちょっと立ち往生しています。WPFカスタムコントロールと直接コンテンツのサポート

私はすでに働いている機能(つまりソート、フィルタリング、標準メニューなど)のいくつかの要素をカプセル化するWPFカスタムコントロールを作成しようとしていますが、繰り返しは避けるために素敵なパッケージです。

とにかく、私は(制御に基づく)カスタムコントロールを作成している、と私は以下のように、GridViewColumns(または実際に任意のコントロール)を追加しようとすると、その後Generic.Xaml

<ResourceDictionary 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:local="clr-namespace:Controls.ListViewExtended"> 
    <Style TargetType="{x:Type local:ListViewExtended}"> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="{x:Type local:ListViewExtended}"> 
        <Border Background="{TemplateBinding Background}" 
          BorderBrush="{TemplateBinding BorderBrush}" 
          BorderThickness="{TemplateBinding BorderThickness}"> 
         <ListView> 
          <ListView.View> 
           <GridView> 
           <!-- Content goes here -->        
           </GridView> 
          </ListView.View> 
         </ListView> 
        </Border> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 
</ResourceDictionary> 

を次のように持っています。 ..

<elv:ListViewExtended> 
    <GridView> 
     <GridViewColumn Width="140" Header="Column 1" /> 
     <GridViewColumn Width="140" Header="Column 2" /> 
     <GridViewColumn Width="140" Header="Column 3" /> 
    </GridView> 
</elv:ListViewExtended> 

私はエラー "...が直接コンテンツをサポートしていません" を取得します。

私はGridViewの追加を可能にする依存関係プロパティを作成しましたが、まだ動作しません。

public static DependencyProperty GridViewProperty; 

public static string GridViewHeader(DependencyObject target) 
{ 
    return (string)target.GetValue(GridViewProperty); 
} 
public static void GridViewHeader(DependencyObject target, string value) 
{ 
    target.SetValue(GridViewProperty, value); 
} 

事前

答えて

2

のおかげではないControlから、ListViewからカスタムコントロールを継承。これにより間違いなくテンプレートを変更することができますが、詳細については、Sacha Barberの記事:Creating and consuming a custom WPF controlなどのドキュメントをお読みください。

あなたの学習に幸運を祈る!

+0

、私は私が使用できるリストビューを取得します。しかし、私はコントロールのすべてのインスタンスではなく、コントロールにスタイルとポップアップメニューを追加できるようにしたいのですか?しかし、それは私にとっては別の質問のように聞こえる。 – Mmarquee

1

私はカスタムコントロールで直接コンテンツをサポートするために、私たちのプロジェクトでは、このシンプルなソリューションを使用します。

ではなく、「制御」の「ユーザーコントロール」をプロジェクトに「CustomControl」を追加して、クラスからこのコントロールを派生:

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

プロジェクトにCustomControlを追加すると、Visual Studio(私は2012を使用しています)は、 "Generic.xaml"という名前のファイルを含むフォルダ "テーマ"を自動的に追加します。 このファイルには、CustomControlのスタイル(テンプレート)を定義するResourceDictionaryがあります。

既にDefaultStyleとして使用されているCustomControlの基本テンプレートがあります。直接コンテンツのサポート場所のために親コンテンツで、このテンプレート内のどこかのContentPresenterはバインディング:

<Style TargetType="{x:Type local:MyCustomControl}"> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="{x:Type local:MyCustomControl}"> 
       <Border Background="{TemplateBinding Background}" 
         BorderBrush="{TemplateBinding BorderBrush}" 
         BorderThickness="{TemplateBinding BorderThickness}"> 
        <ContentPresenter Content="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Content}" /> 
       </Border> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 

を今、それはあなたのCustomControlにコンテンツを追加することが可能です。このことができます

<Window x:Class="MyApplication.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:controls="clr-namespace:MyApplication" 
     Title="MainWindow" Height="350" Width="525"> 
    <Grid> 
     <controls:MyCustomControl> 
       <TextBlock>Hello</TextBlock> 
     </controls:MyCustomControl> 
    </Grid> 
</Window> 

希望を!

13

すべてContentPropertyAttributeを指定する必要があります。

[ContentProperty("MainContent")] 
public class GroupPanel : Control 
{ 
    public GroupPanel() 
    { 
     DefaultStyleKey = typeof(GroupPanel); 
    } 

    public object MainContent 
    { 
     get { return GetValue(MainContentProperty); } 
     set { SetValue(MainContentProperty, value); } 
    } 

    public static readonly DependencyProperty MainContentProperty = 
     DependencyProperty.Register("MainContent", typeof(object), typeof(GroupPanel), null); 
} 

参考:私はそれを試したし、それが動作http://msdn.microsoft.com/en-us/library/system.windows.markup.contentpropertyattribute(v=vs.90).aspx

関連する問題