默认组合框模板。弹出宽度

时间:2011-01-29 04:52:15

标签: wpf silverlight templates combobox

我想在默认的组合框模板中添加一些按钮,所以我覆盖了它的模板。

我用这个:

 <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="l:RefreshableComboBox">
                <Grid Name="MainGrid" SnapsToDevicePixels="True">

                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*" />
                        <ColumnDefinition Width="0" MinWidth="{DynamicResource {x:Static SystemParameters.VerticalScrollBarWidthKey}}" />
                    </Grid.ColumnDefinitions>

                    <Popup  IsOpen="{Binding Path=IsDropDownOpen,  RelativeSource={RelativeSource TemplatedParent}}" Placement="Bottom" PopupAnimation="{DynamicResource {x:Static SystemParameters.ComboBoxPopupAnimationKey}}" AllowsTransparency="True" Name="PART_Popup" Margin="1,1,1,1" Grid.ColumnSpan="2" >
                        <mwt:SystemDropShadowChrome Color="#00FFFFFF" Name="Shdw" MinWidth="0" MaxHeight="{TemplateBinding ComboBox.MaxDropDownHeight}">
                            <Border BorderThickness="1,1,1,1" BorderBrush="{DynamicResource {x:Static SystemColors.WindowFrameBrushKey}}" Background="{DynamicResource {x:Static SystemColors.WindowBrushKey}}" Name="DropDownBorder">
                                <ScrollViewer Name="DropDownScrollViewer">
                                    <Grid RenderOptions.ClearTypeHint="Enabled">
                                        <Canvas Width="0" Height="0" HorizontalAlignment="Left" VerticalAlignment="Top">

                                            <Rectangle Fill="{x:Null}" Name="OpaqueRect" Width="Auto" Height="Auto" />
                                        </Canvas>

                                        <ItemsPresenter Name="ItemsPresenter" SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}" KeyboardNavigation.DirectionalNavigation="Contained" />

                                    </Grid>
                                </ScrollViewer>

                            </Border>

                        </mwt:SystemDropShadowChrome>
                    </Popup>
                    <ToggleButton x:Name="ToggleButton" IsChecked="{Binding Path=IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}"  BorderBrush="{TemplateBinding Border.BorderBrush}" Background="{TemplateBinding Panel.Background}" Grid.ColumnSpan="2">
                        <ToggleButton.Style>
                            <Style TargetType="ToggleButton">
                                <Style.Resources>
                                    <ResourceDictionary />
                                </Style.Resources>
                                <Setter Property="FrameworkElement.OverridesDefaultStyle">
                                    <Setter.Value>
                                        <s:Boolean>True</s:Boolean>
                                    </Setter.Value>
                                </Setter>
                                <Setter Property="KeyboardNavigation.IsTabStop">
                                    <Setter.Value>
                                        <s:Boolean>False</s:Boolean>
                                    </Setter.Value>
                                </Setter>
                                <Setter Property="UIElement.Focusable">
                                    <Setter.Value>
                                        <s:Boolean>False</s:Boolean>
                                    </Setter.Value>
                                </Setter>
                                <Setter Property="ButtonBase.ClickMode">
                                    <Setter.Value>
                                        <x:Static Member="ClickMode.Press" />
                                    </Setter.Value>
                                </Setter>
                                <Setter Property="Control.Template">
                                    <Setter.Value>
                                        <ControlTemplate TargetType="ToggleButton">
                                            <mwt:ButtonChrome Background="{TemplateBinding Panel.Background}" 
                                              BorderBrush="{TemplateBinding Border.BorderBrush}" 
                                              RenderMouseOver="{TemplateBinding UIElement.IsMouseOver}" 
                                              RenderPressed="{TemplateBinding ButtonBase.IsPressed}" 
                                              Name="Chrome" SnapsToDevicePixels="True">
                                                <Grid Width="{DynamicResource {x:Static SystemParameters.VerticalScrollBarWidthKey}}" HorizontalAlignment="Right">
                                                    <Path Data="M0,0L3.5,4 7,0z" Fill="#FF000000" Name="Arrow" Margin="3,1,0,0" 
                                          HorizontalAlignment="Center" VerticalAlignment="Center" />
                                                </Grid>
                                            </mwt:ButtonChrome>
                                            <ControlTemplate.Triggers>
                                                <Trigger Property="ToggleButton.IsChecked">
                                                    <Setter Property="mwt:ButtonChrome.RenderPressed" TargetName="Chrome">
                                                        <Setter.Value>
                                                            <s:Boolean>True</s:Boolean>
                                                        </Setter.Value>
                                                    </Setter>
                                                    <Trigger.Value>
                                                        <s:Boolean>True</s:Boolean>
                                                    </Trigger.Value>
                                                </Trigger>
                                                <Trigger Property="UIElement.IsEnabled">
                                                    <Setter Property="Shape.Fill" TargetName="Arrow">
                                                        <Setter.Value>
                                                            <SolidColorBrush>#FFAFAFAF</SolidColorBrush>
                                                        </Setter.Value>
                                                    </Setter>
                                                    <Trigger.Value>
                                                        <s:Boolean>False</s:Boolean>
                                                    </Trigger.Value>
                                                </Trigger>
                                            </ControlTemplate.Triggers>
                                        </ControlTemplate>
                                    </Setter.Value>
                                </Setter>
                            </Style>
                        </ToggleButton.Style>
                    </ToggleButton>
                    <ContentPresenter Content="{TemplateBinding ComboBox.SelectionBoxItem}" ContentTemplate="{TemplateBinding ComboBox.SelectionBoxItemTemplate}" ContentStringFormat="{TemplateBinding ComboBox.SelectionBoxItemStringFormat}" Margin="{TemplateBinding Control.Padding}" HorizontalAlignment="{TemplateBinding Control.HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding Control.VerticalContentAlignment}" SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}" IsHitTestVisible="False" />
                </Grid>
                <ControlTemplate.Triggers>
                    <Trigger Property="Popup.HasDropShadow" SourceName="PART_Popup">
                        <Setter Property="FrameworkElement.Margin" TargetName="Shdw">
                            <Setter.Value>
                                <Thickness>0,0,5,5</Thickness>
                            </Setter.Value>
                        </Setter>
                        <Setter Property="mwt:SystemDropShadowChrome.Color" TargetName="Shdw">
                            <Setter.Value>
                                <Color>#71000000</Color>
                            </Setter.Value>
                        </Setter>
                        <Trigger.Value>
                            <s:Boolean>True</s:Boolean>
                        </Trigger.Value>
                    </Trigger>
                    <Trigger Property="ItemsControl.HasItems">
                        <Setter Property="FrameworkElement.Height" TargetName="DropDownBorder">
                            <Setter.Value>
                                <s:Double>95</s:Double>
                            </Setter.Value>
                        </Setter>
                        <Trigger.Value>
                            <s:Boolean>False</s:Boolean>
                        </Trigger.Value>
                    </Trigger>
                    <Trigger Property="UIElement.IsEnabled">
                        <Setter Property="TextElement.Foreground">
                            <Setter.Value>
                                <DynamicResource ResourceKey="{x:Static SystemColors.GrayTextBrushKey}" />
                            </Setter.Value>
                        </Setter>
                        <Setter Property="Panel.Background">
                            <Setter.Value>
                                <SolidColorBrush>#FFF4F4F4</SolidColorBrush>
                            </Setter.Value>
                        </Setter>
                        <Trigger.Value>
                            <s:Boolean>False</s:Boolean>
                        </Trigger.Value>
                    </Trigger>
                    <Trigger Property="ItemsControl.IsGrouping">
                        <Setter Property="ScrollViewer.CanContentScroll">
                            <Setter.Value>
                                <s:Boolean>False</s:Boolean>
                            </Setter.Value>
                        </Setter>
                        <Trigger.Value>
                            <s:Boolean>True</s:Boolean>
                        </Trigger.Value>
                    </Trigger>
                    <Trigger Property="ScrollViewer.CanContentScroll" SourceName="DropDownScrollViewer">
                        <Setter Property="Canvas.Top" TargetName="OpaqueRect">
                            <Setter.Value>
                                <Binding Path="VerticalOffset" ElementName="DropDownScrollViewer" />
                            </Setter.Value>
                        </Setter>
                        <Setter Property="Canvas.Left" TargetName="OpaqueRect">
                            <Setter.Value>
                                <Binding Path="HorizontalOffset" ElementName="DropDownScrollViewer" />
                            </Setter.Value>
                        </Setter>
                        <Trigger.Value>
                            <s:Boolean>False</s:Boolean>
                        </Trigger.Value>
                    </Trigger>
                </ControlTemplate.Triggers>



            </ControlTemplate>
        </Setter.Value>
    </Setter>

由于某种原因,我的弹出窗口(PART_Popup)与toggleButton的宽度不同。它的宽度足以显示项目。如果我不覆盖模板弹出窗口的宽度与toggleButton相同。所以我无法理解为什么我不能用标准模板

获得相同的情况

1 个答案:

答案 0 :(得分:1)

MinWidth的{​​{1}}从mwt:SystemDropShadowChrome更改为:

0