C# 自定义组合框选择显示
我目前有一个定制的组合框,允许从下拉列表中选择图像,并在组合框的切换按钮内显示图像。它工作正常,但我有一个问题。如果我从代码或xaml(例如,使用selectedIndex)设置当前所选项目,则按钮中的图像显示错误(按钮中仅显示一半图像)。如果我通过下拉菜单手动选择它,效果很好(图像拉伸到按钮大小)。事实上,只需点击下拉列表似乎就能解决问题 我的问题是,当你点击组合框的切换按钮时会发生什么。这样我就可以复制它并使它工作 图片:C# 自定义组合框选择显示,c#,wpf,image,combobox,C#,Wpf,Image,Combobox,我目前有一个定制的组合框,允许从下拉列表中选择图像,并在组合框的切换按钮内显示图像。它工作正常,但我有一个问题。如果我从代码或xaml(例如,使用selectedIndex)设置当前所选项目,则按钮中的图像显示错误(按钮中仅显示一半图像)。如果我通过下拉菜单手动选择它,效果很好(图像拉伸到按钮大小)。事实上,只需点击下拉列表似乎就能解决问题 我的问题是,当你点击组合框的切换按钮时会发生什么。这样我就可以复制它并使它工作 图片: 抱歉,我还不能添加注释,但是在代码中手动调用invalidat
抱歉,我还不能添加注释,但是在代码中手动调用invalidate()是否也会显示完整图像? 如果是,则可能正在异步将图像加载到复选框中,而在加载尚未完成时首次调用invalidate(), 然后异步完成加载,因此每次在初始绘制后刷新视图时,视图都可以正常工作。
这只是一个假设。你能发布一些图片来说明症状吗?将它们上传到一些网站并提供链接,我可以帮助您将它们导入Stackoverflow。另外,发布相关的XAML标记和代码(如果有)。我不知道为什么组合框会有切换按钮,似乎这不是默认属性。但是下面是组合框生成的事件:如果您想测试它们,通常可以从代码中触发这些事件。添加的代码和屏幕快照在从代码中设置所选索引后,我尝试调用invalidate,但仍然会遇到相同的问题。图像没有拉伸嗯,我可以看看你是如何通过代码将图像加载到组合框切换按钮的吗?在上面的代码中,图像是comboboxitem的内容,传递到使用SpellComboBox中的contentpresenter的样式中的切换按钮您可以尝试将切换按钮的水平ContentAlignment和垂直ContentAlignment设置为stretch吗?是的,我遇到了相同的问题
<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>