2017-03-28 29 views
0

以下はコンボボックスのテンプレートです。見た目は素晴らしく、バインディングはうまくいきます。しかし、リスト内の項目を選択すると、選択した項目がボックス内に正しく表示されず、代わりに長い文字列値(common.Dto.CityDto)が得られます。これは実際にDtoのファイル位置ですリストをバインドするために。奇妙な。 ありがとう!wpfコンボボックステンプレート選択項目が正しく表示されない

<ControlTemplate x:Key="GrayComboBox" TargetType="{x:Type ComboBox}"> 
     <Border BorderBrush="#dee0e3" BorderThickness="2" Background="#dee0e3" SnapsToDevicePixels="True" CornerRadius="7"> 
      <Grid FlowDirection="RightToLeft"> 
       <Themes:ClassicBorderDecorator x:Name="Border" BorderBrush="#00000000" BorderThickness="2" BorderStyle="Sunken" Background="#dee0e3"> 
        <Popup x:Name="PART_Popup" AllowsTransparency="True" Focusable="False" IsOpen="{TemplateBinding IsDropDownOpen}" PopupAnimation="{DynamicResource {x:Static SystemParameters.ComboBoxPopupAnimationKey}}" Placement="Bottom"> 
         <Themes:SystemDropShadowChrome x:Name="Shdw" Color="Transparent" MaxHeight="{TemplateBinding MaxDropDownHeight}" MinWidth="{Binding ActualWidth, ElementName=Border}"> 
          <Border x:Name="DropDownBorder" BorderBrush="#dee0e3" BorderThickness="1" Background="#dee0e3"> 
           <ScrollViewer x:Name="DropDownScrollViewer" > 
            <Grid RenderOptions.ClearTypeHint="Enabled" > 
             <Canvas HorizontalAlignment="Left" Height="0" VerticalAlignment="Top" Width="0"> 
              <Rectangle x:Name="OpaqueRect" Fill="{Binding Background, ElementName=DropDownBorder}" Height="{Binding ActualHeight, ElementName=DropDownBorder}" Width="{Binding ActualWidth, ElementName=DropDownBorder}"/> 
             </Canvas> 
             <ItemsPresenter x:Name="ItemsPresenter" KeyboardNavigation.DirectionalNavigation="Contained" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/> 
            </Grid> 
           </ScrollViewer> 
          </Border> 
         </Themes:SystemDropShadowChrome> 
        </Popup> 
       </Themes:ClassicBorderDecorator> 
       <DockPanel Margin="2"> 
        <FrameworkElement DockPanel.Dock="Right" Width="{DynamicResource {x:Static SystemParameters.VerticalScrollBarWidthKey}}"/> 
        <Border x:Name="SelectedItemBorder" Margin="{TemplateBinding Padding}"> 
         <ContentPresenter ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}" Content="{TemplateBinding SelectionBoxItem}" ContentStringFormat="{TemplateBinding SelectionBoxItemStringFormat}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="1" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/> 
        </Border> 
       </DockPanel> 
       <ToggleButton ClickMode="Press" HorizontalAlignment="Right" Focusable="False" IsChecked="{Binding IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}" Margin="2" MinWidth="0" MinHeight="0" Width="Auto"> 
        <ToggleButton.Style> 
         <Style TargetType="{x:Type ToggleButton}"> 
          <Setter Property="MinWidth" Value="0"/> 
          <Setter Property="MinHeight" Value="0"/> 
          <Setter Property="Width" Value="Auto"/> 
          <Setter Property="Height" Value="Auto"/> 
          <Setter Property="Background" Value="Transparent"/> 
          <Setter Property="BorderBrush" Value="#00000000"/> 
          <Setter Property="BorderThickness" Value="2"/> 
          <Setter Property="Margin" Value="2,2,2,2" /> 
          <Setter Property="Template"> 
           <Setter.Value> 
            <ControlTemplate TargetType="{x:Type ToggleButton}"> 
             <DockPanel Background="{TemplateBinding Background}" LastChildFill="False" SnapsToDevicePixels="True"> 
              <Themes:ClassicBorderDecorator x:Name="Border" BorderBrush="Transparent" BorderThickness="{TemplateBinding BorderThickness}" BorderStyle="AltRaised" Background="Transparent" DockPanel.Dock="Right" Width="{DynamicResource {x:Static SystemParameters.VerticalScrollBarWidthKey}}"> 
               <!--<Path Data="M0,0L3.5,4 7,0z" Fill="{TemplateBinding Foreground}" HorizontalAlignment="Center" VerticalAlignment="Center"/>--> 
               <fa:ImageAwesome Icon="CaretDown" Foreground="DarkGray" Width="13" Height="10" HorizontalAlignment="Center" VerticalAlignment="Center" /> 
              </Themes:ClassicBorderDecorator> 
             </DockPanel> 
             <ControlTemplate.Triggers> 
              <Trigger Property="IsChecked" Value="True"> 
               <Setter Property="BorderStyle" TargetName="Border" Value="AltPressed"/> 
              </Trigger> 
             </ControlTemplate.Triggers> 
            </ControlTemplate> 
           </Setter.Value> 
          </Setter> 
          <Style.Triggers> 
           <Trigger Property="IsEnabled" Value="False"> 
            <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlDarkBrushKey}}"/> 
           </Trigger> 
          </Style.Triggers> 
         </Style> 
        </ToggleButton.Style> 
       </ToggleButton> 
      </Grid> 
     </Border> 
     <ControlTemplate.Triggers> 
      <MultiTrigger> 
       <MultiTrigger.Conditions> 
        <Condition Property="IsSelectionBoxHighlighted" Value="True"/> 
        <Condition Property="IsDropDownOpen" Value="False"/> 
       </MultiTrigger.Conditions> 
       <Setter Property="Foreground" Value="#006165"/> 
      </MultiTrigger> 
      <Trigger Property="IsSelectionBoxHighlighted" Value="True"> 
       <Setter Property="Background" TargetName="SelectedItemBorder" Value="#dee0e3"/> 
      </Trigger> 
      <Trigger Property="HasItems" Value="False"> 
       <Setter Property="MinHeight" TargetName="DropDownBorder" Value="95"/> 
      </Trigger> 
      <Trigger Property="IsEnabled" Value="False"> 
       <Setter Property="Foreground" Value="DarkGray"/> 
       <Setter Property="Background" Value="#dee0e3"/> 
      </Trigger> 
      <MultiTrigger> 
       <MultiTrigger.Conditions> 
        <Condition Property="IsGrouping" Value="True"/> 
        <Condition Property="VirtualizingPanel.IsVirtualizingWhenGrouping" Value="False"/> 
       </MultiTrigger.Conditions> 
       <Setter Property="ScrollViewer.CanContentScroll" Value="False"/> 
      </MultiTrigger> 
      <Trigger Property="HasDropShadow" SourceName="PART_Popup" Value="True"> 
       <Setter Property="Margin" TargetName="Shdw" Value="0,0,5,5"/> 
       <Setter Property="Color" TargetName="Shdw" Value="#71000000"/> 
      </Trigger> 
      <Trigger Property="CanContentScroll" SourceName="DropDownScrollViewer" Value="False"> 
       <Setter Property="Canvas.Top" TargetName="OpaqueRect" Value="{Binding VerticalOffset, ElementName=DropDownScrollViewer}"/> 
       <Setter Property="Canvas.Left" TargetName="OpaqueRect" Value="{Binding HorizontalOffset, ElementName=DropDownScrollViewer}"/> 
      </Trigger> 
     </ControlTemplate.Triggers> 
    </ControlTemplate> 
+0

。 DisplayNameをドットオブジェクトに表示するプロパティに設定します。 – Peter

+0

何もありません – GCamel

+0

GCamel-これはコンボボックスにあります:SelectedValue = "{バインディングModel.CityId、UpdateSourceTrigger = PropertyChanged}" SelectedValuePath = "Id" DisplayMemberPath = "表示" – Yitta

答えて

0

あなたはContentPresenterContentTemplateSelectorプロパティを設定するのを忘れ:

<ContentPresenter ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}" ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}" Content="{TemplateBinding SelectionBoxItem}" ContentStringFormat="{TemplateBinding SelectionBoxItemStringFormat}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="1" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/> 
テンプレートメイトあなたのテンプレートを使用して行うには
+0

うまく働いてくれてありがとうございました! – Yitta

関連する問題