自定义组合框选择显示

时间:2015-03-20 02:19:05

标签: c# wpf image combobox

我目前有一个定制的组合框允许从下拉列表中选择一个图像,并在组合框的切换按钮内显示图像。它工作正常,但我有一个问题。如果我从代码或xaml(例如selectedIndex)设置我当前选择的项目,则按钮中图像的显示是错误的(按钮中只显示一半图像)。如果我通过下拉菜单手动选择它,它工作正常(图像拉伸到按钮大小)。事实上,只需点击下拉列表就可以解决问题。

我的问题是,当你点击组合框的togglebutton时会发生什么。这样我就可以复制它并使其工作。

图片:http://postimg.org/image/tlwg0gdq3/

<ComboBox Name="Spell1ComboBox" Style="{StaticResource SpellComboBox}" IsSynchronizedWithCurrentItem="True" Width="55" DockPanel.Dock="Left" SelectionChanged="OnSelectionChanged" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch">
<controls:MyComboBoxItem Tag="Rage" Description="Le mode rage augmente considérablement la cadence de tir du vaisseau." Grid.Row="0" Grid.Column="0">
    <Image Source="/AsteroidsInterface;component/Media/spell1.jpg" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Stretch="Fill"/>
</controls:MyComboBoxItem>
<controls:MyComboBoxItem Tag="Chaîne" Description="Créer une chaîne laser entre les deux joueurs pour 5 secondes. Tout les astéroides entrant en contact avec la chaine sont détruits. Si un seul joueur est présent, celle-ci s'attache à la station la plus proche." Grid.Row="0" Grid.Column="1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
    <Image Source="/AsteroidsInterface;component/Media/spell2.jpg" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Stretch="Fill"></Image>
</controls:MyComboBoxItem>
<controls:MyComboBoxItem Tag="Bouclier" Description="Le vaisseau devient invulnérable et détruit les astéroides avec lesquelles il entre en contact." Grid.Row="0" Grid.Column="2" >
    <Image Source="/AsteroidsInterface;component/Media/spell3.jpg" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Stretch="Fill"></Image>
</controls:MyComboBoxItem>
<controls:MyComboBoxItem Tag="Réparation" Description="Les tirs du joueur peuvent redonner des points de vie aux stations. Peux être combiné avec rage pour augmenter l'efficacité. " Grid.Row="1" Grid.Column="0" >
    <Image Source="/AsteroidsInterface;component/Media/spell4.jpg" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Stretch="Fill"></Image>
</controls:MyComboBoxItem>
<controls:MyComboBoxItem Tag="Téléportation" Description="Permet au vaisseau de se téléporter dans la direction qu'il face." Grid.Row="1" Grid.Column="1" >
    <Image Source="/AsteroidsInterface;component/Media/spell5.jpg" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Stretch="Fill"></Image>
</controls:MyComboBoxItem>

<Style TargetType="{x:Type controls:MyComboBoxItem}">
        <Setter Property="SnapsToDevicePixels" Value="true" />
        <Setter Property="VerticalAlignment" Value="Stretch" />
        <Setter Property="FontSize" Value="16" />
        <Setter Property="FontWeight" Value="Bold" />
        <Setter Property="Template">
            <Setter.Value>
            <ControlTemplate TargetType="controls:MyComboBoxItem">
                <Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
                    <Border
                        Name="Border"
                        Padding="0"
                        Height="50" 
                        Width="50"
                        Margin="0"
                        BorderThickness="0"
                        CornerRadius="0"
                        Background="Transparent"
                        BorderBrush="Orange" 
                        HorizontalAlignment="Center" VerticalAlignment="Center">
                        <ContentPresenter/>
                    </Border>
                    <Popup x:Name="Popup"
                             Placement="Right"
                             HorizontalOffset="-4"
                             IsOpen="{Binding ElementName=Border, Path=IsMouseOver, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"
                             AllowsTransparency="True"
                             Focusable="False"
                             PopupAnimation="None">
                        <DockPanel Background="Orange" Width="100">
                            <Label Content="{TemplateBinding Tag}" FontSize="12" Foreground="Black" FontFamily="Bold" DockPanel.Dock="Top"></Label>
                            <TextBlock Text="{TemplateBinding Description}" 
                                               Foreground="Black" 
                                               FontSize="10"
                                               DockPanel.Dock="Top" TextWrapping="Wrap"
                                               Padding="10,3,3,3"></TextBlock>
                        </DockPanel>
                    </Popup>
                </Grid>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsHighlighted" Value="true">
                        <Setter TargetName="Border" Property="BorderBrush" Value="{StaticResource UltimateBlueBrush}"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
        </Setter>
    </Style>

    <Style x:Key="SpellComboBoxToggleButton" TargetType="ToggleButton">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ToggleButton">
                    <Grid Height="60" Width="55">
                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup x:Name="CommonStates">
                                <VisualState x:Name="Normal" />
                                <VisualState x:Name="MouseOver"/>
                                <VisualState x:Name="Pressed" />
                                <VisualState x:Name="Disabled">
                                </VisualState>
                            </VisualStateGroup>
                            <VisualStateGroup x:Name="CheckStates">
                                <VisualState x:Name="Checked">
                                    <Storyboard>
                                        <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)"
                                          Storyboard.TargetName="Border">
                                            <EasingColorKeyFrame KeyTime="0" Value="Orange" />
                                        </ColorAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                                <VisualState x:Name="Unchecked" />
                                <VisualState x:Name="Indeterminate" />
                            </VisualStateGroup>
                        </VisualStateManager.VisualStateGroups>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="55"/>
                        </Grid.ColumnDefinitions>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="55"></RowDefinition>
                            <RowDefinition Height="5"></RowDefinition>
                        </Grid.RowDefinitions>
                        <Border
                          x:Name="Border"
                          Grid.RowSpan="2"
                          CornerRadius="5"
                          BorderBrush="{StaticResource UltimateBlueBrush}"
                          BorderThickness="0" >
                            <Border.Background>
                                <SolidColorBrush Color="{StaticResource UltimateBlue}"/>
                            </Border.Background>
                        </Border>
                        <Border 
                          Grid.Row="0"
                          Width="55"
                          Height="55"
                          CornerRadius="5"
                          BorderBrush="Transparent"
                          BorderThickness="2">
                            <ContentPresenter
                                IsHitTestVisible="False"
                                Content="{TemplateBinding Content}"
                                VerticalAlignment="Stretch"
                                HorizontalAlignment="Stretch" />
                        </Border>
                        <Grid Grid.Row="1"  HorizontalAlignment="Center" VerticalAlignment="Center" >
                            <Path x:Name="Arrow"     
                                  Width="5"
                                  Height="3"
                                  Fill="Orange"
                                  HorizontalAlignment="Center"
                                  VerticalAlignment="Center"
                                  Data="M 0 0 L 4 4 L 8 0 Z"/>
                        </Grid>
                    </Grid>

                    <ControlTemplate.Triggers>
                        <Trigger Property="IsEnabled" Value="False">
                            <Setter TargetName="Border" Property="Background" Value="LightGray" />
                            <Setter TargetName="Border" Property="BorderBrush" Value="Gray" />
                            <Setter Property="Foreground" Value="Gray"/>
                            <Setter TargetName="Arrow" Property="Fill" Value="Gray" />
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

    <Style x:Key="SpellComboBox" TargetType="ComboBox">
        <Setter Property="Foreground" Value="Gray" />
        <Setter Property="BorderBrush" Value="Gray" />
        <Setter Property="Background" Value="Red" />
        <Setter Property="SnapsToDevicePixels" Value="true"/>
        <Setter Property="OverridesDefaultStyle" Value="true"/>
        <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto"/>
        <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto"/>
        <Setter Property="ScrollViewer.CanContentScroll" Value="true"/>
        <Setter Property="FontWeight" Value="Bold" />
        <Setter Property="MinWidth" Value="60"/>
        <Setter Property="MinHeight" Value="55"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ComboBox">
                    <Grid>
                        <ToggleButton
                                Name="ToggleButton"
                                Style="{StaticResource SpellComboBoxToggleButton}"
                                Grid.Column="0"
                                Focusable="false"
                                IsChecked="{Binding Path=IsDropDownOpen,Mode=TwoWay,RelativeSource={RelativeSource TemplatedParent}}"
                                ClickMode="Press">
                            <ContentPresenter
                                Name="ContentSite"
                                IsHitTestVisible="False"
                                Content="{TemplateBinding SelectionBoxItem}"
                                ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}"
                                VerticalAlignment="Stretch"
                                HorizontalAlignment="Stretch" >                                
                            </ContentPresenter>
                        </ToggleButton>
                        <Popup x:Name="Popup"
                             Placement="Bottom"
                             HorizontalOffset="-5"
                             VerticalOffset="-3"
                             IsOpen="{TemplateBinding IsDropDownOpen}"
                             AllowsTransparency="True"
                             Focusable="False"
                             PopupAnimation="Slide">
                            <Grid x:Name="DropDown"
                              SnapsToDevicePixels="True"
                              MinWidth="174"
                              MinHeight="116"
                              MaxHeight="116">
                                <Border x:Name="DropDownBorder"
                                    BorderThickness="0" CornerRadius="5">
                                    <Border.BorderBrush>
                                        <SolidColorBrush Color="Orange" />
                                    </Border.BorderBrush>
                                    <Border.Background>
                                        <SolidColorBrush Color="Orange" />
                                    </Border.Background>
                                    <Grid Margin="2,2,2,2" IsItemsHost="True" 
                                                 KeyboardNavigation.DirectionalNavigation="Contained">
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition ></ColumnDefinition>
                                            <ColumnDefinition ></ColumnDefinition>
                                            <ColumnDefinition ></ColumnDefinition>
                                        </Grid.ColumnDefinitions>
                                        <Grid.RowDefinitions>
                                            <RowDefinition></RowDefinition>
                                            <RowDefinition ></RowDefinition>
                                        </Grid.RowDefinitions>
                                    </Grid>
                                </Border>
                            </Grid>
                        </Popup>
                    </Grid>
                    <ControlTemplate.Triggers>

                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

1 个答案:

答案 0 :(得分:0)

抱歉,我还无法添加评论,但是在您的代码中手动调用invalidate()也会显示完整的图像吗? 如果是,可能正在异步地将图像加载到复选框,而在加载甚至没有完成时首先调用invalidate(), 然后加载异步完成,因此每次初始绘制后刷新视图都可以正常工作。 这只是一个假设。