2016-12-06 9 views
0

サンプルComboBoxがこのように定義されているとします。私がやりたい何WPF:ComboBox:Style/ControlTemplateでDataTemplateバインディングを公開する

<ComboBox ItemsSource="{Binding Path=Bikes}" 
      HorizontalAlignment="Stretch"> 
    <ComboBox.ItemTemplate> 
     <DataTemplate> 
      <StackPanel Orientation="Horizontal"> 
       <Ellipse Fill="{DynamicResource AccentColorBrush}" 
          Height="15" 
          Width="15" 
          VerticalAlignment="Center"/> 
       <TextBlock Text="{Binding Type}" 
          Margin="15, 0, 0, 0" 
          FontWeight="SemiBold" 
          Foreground="{DynamicResource AccentColorBrush}" 
          VerticalAlignment="Center"/> 
      </StackPanel> 
     </DataTemplate> 
    </ComboBox.ItemTemplate> 
</ComboBox> 

Styleでそれをラップし、私は別のプロパティに私がComboBoxを使用するたびにバインドすることができるように、DataTemplateComboBoxText財産のItemSourceプロパティを公開することです。

+0

次に**スタイル**でラップすると**エラー**が発生しましたか? –

答えて

1

ViewModel-Firstアプローチを使用してコンボアイテムのテンプレートを選択するコンボボックススタイルを作成することをお勧めします。したがって、特定のビューモデルに関連する多数のデータテンプレートが存在します。

<Window x:Class="WpfViewConstructorCall.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:local="clr-namespace:WpfViewConstructorCall" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    Title="MainWindow" 
    Width="525" 
    Height="350" 
    mc:Ignorable="d"> 
<Window.Resources> 
    <!--Combo-item data-template wich is defining the final item view--> 
    <DataTemplate x:Key="ComboItemContentTemplate"> 
     <StackPanel Orientation="Horizontal"> 
      <Ellipse Width="15" 
        Height="15" 
        VerticalAlignment="Center" 
        Fill="GreenYellow" /> 
      <TextBlock Margin="15, 0, 0, 0" 
         VerticalAlignment="Center" 
         FontWeight="SemiBold" 
         Foreground="Red" 
         Text="{Binding}" /> 
     </StackPanel> 
    </DataTemplate> 
    <!--Combo-item data-template wich relates to ItemA view-model--> 
    <DataTemplate DataType="{x:Type local:ItemA}"> 
     <ContentControl Content="{Binding Name}" 
         ContentTemplate="{StaticResource ComboItemContentTemplate}" /> 
    </DataTemplate> 
    <!--Combo-item data-template wich relates to ItemB view-model--> 
    <DataTemplate DataType="{x:Type local:ItemB}"> 
     <ContentControl Content="{Binding Kind}" 
         ContentTemplate="{StaticResource ComboItemContentTemplate}" /> 
    </DataTemplate> 
    <!--Combo-item data-template wich relates to ItemC view-model--> 
    <DataTemplate DataType="{x:Type local:ItemC}"> 
     <ContentControl Content="{Binding Type}" 
         ContentTemplate="{StaticResource ComboItemContentTemplate}" /> 
    </DataTemplate> 
    <!--main Combo-item data-template--> 
    <DataTemplate x:Key="ComboItemDataTemplate"> 
     <ContentControl Content="{Binding}" /> 
    </DataTemplate> 
    <!--Combo style--> 
    <Style x:Key="ComboBoxStyle" 
      TargetType="ComboBox"> 
     <Setter Property="HorizontalAlignment" Value="Stretch" /> 
     <Setter Property="ItemTemplate" Value="{StaticResource ComboItemDataTemplate}" /> 
    </Style> 
</Window.Resources> 
<Grid> 
    <Grid.DataContext> 
     <local:GridDataContext /> 
    </Grid.DataContext> 
    <StackPanel> 
     <!--Combo(s) with specific source definition--> 
     <ComboBox x:Name="A" 
        DisplayMemberPath="Name" 
        ItemsSource="{Binding ItemsA}" 
        SelectedValuePath="Name" 
        Style="{StaticResource ComboBoxStyle}" /> 
     <ComboBox x:Name="B" 
        DisplayMemberPath="Kind" 
        ItemsSource="{Binding ItemsB}" 
        SelectedValuePath="Kind" 
        Style="{StaticResource ComboBoxStyle}" /> 
     <ComboBox x:Name="C" 
        DisplayMemberPath="Type" 
        ItemsSource="{Binding ItemsC}" 
        SelectedValuePath="Type" 
        Style="{StaticResource ComboBoxStyle}" /> 
    </StackPanel> 
</Grid></Window> 

場合は、あなたがその特定のビューモデルに関係し、そのビューを定義するデータ・テンプレートを追加する追加のコンボアイテムのビューモデルを持っています。

例がさらに必要な場合はお知らせください。

よろしくお願いいたします。

+0

もう1つ私の心に来るもの。 1つ以上の要素があり、異なるプロパティをバインドしたい場合、そのようなテンプレートにバインドする方法。例: ''ここで '

+0

ビューモデルで自由に管理できます。各要素が異なるプロパティにバインドされている場合、ここでのコンテンツ制御は、ビューモデルの最終ビューを提供するためのプロキシのためのみです。 – Ilan

関連する問題