在我的WPF应用程序UserControl中,我想设置组合框的背景。在寻求大量帮助和教程之后,我可以按预期实现它,但现在无法在文本中看到所选项目。我的意思是,所选项目可以在下拉列表中看到,但在未单击任何拖放时不会在组合框中看到。 我尝试了很多变化来设置文本的前景,但没有成功。也尝试通过不同的网站,但无法使一切工作。自从几天以来,我就陷入了困境。以下是我的代码。
<LinearGradientBrush x:Key="NormalBrush" StartPoint="0,0" EndPoint="0,1">
<GradientBrush.GradientStops>
<GradientStopCollection>
<GradientStop Color="#106594" Offset="0.0"/>
<GradientStop Color="LightYellow" Offset="1.0"/>
</GradientStopCollection>
</GradientBrush.GradientStops>
</LinearGradientBrush>
<SolidColorBrush x:Key="WindowBackgroundBrush" Color="#106594" />
<ControlTemplate x:Key="ComboBoxToggleButton" TargetType="ToggleButton">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition Width="20" />
</Grid.ColumnDefinitions>
<Border x:Name="Border" Grid.ColumnSpan="2" CornerRadius="2"
Background="{StaticResource NormalBrush}" BorderThickness="1" />
<Border Grid.Column="0" CornerRadius="2,0,0,2" Margin="1"
Background="{StaticResource WindowBackgroundBrush}" BorderThickness="0,0,1,0" />
<Path x:Name="Arrow" Grid.Column="1" HorizontalAlignment="Center" VerticalAlignment="Center"
Data="M 0 0 L 4 4 L 8 0 Z" Fill="Black"/>
</Grid>
</ControlTemplate>
<ControlTemplate x:Key="ComboBoxTextBox" TargetType="TextBox">
<Border x:Name="PART_ContentHost" Focusable="False" Background="{TemplateBinding Background}" />
</ControlTemplate>
<Style x:Key="{x:Type ComboBox}" TargetType="ComboBox">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ComboBox">
<Grid>
<ToggleButton Name="ToggleButton" Template="{StaticResource ComboBoxToggleButton}"
Grid.Column="2" Focusable="false"
IsChecked="{Binding Path=IsDropDownOpen,Mode=TwoWay,RelativeSource={RelativeSource TemplatedParent}}"
ClickMode="Press" >
</ToggleButton>
<ContentPresenter Name="ContentSite" IsHitTestVisible="False"
Margin="3,3,23,3" VerticalAlignment="Center" HorizontalAlignment="Left" />
<TextBox x:Name="PART_EditableTextBox" Style="{x:Null}"
Template="{StaticResource ComboBoxTextBox}" HorizontalAlignment="Left"
VerticalAlignment="Center" Margin="3,3,23,3" Focusable="True"
Background="Transparent" Foreground="{TemplateBinding Foreground}" Visibility="Hidden"
IsReadOnly="{TemplateBinding IsReadOnly}"/>
<Popup Name="Popup" Placement="Bottom" IsOpen="{TemplateBinding IsDropDownOpen}"
AllowsTransparency="True" Focusable="False" PopupAnimation="Slide">
<Grid Name="DropDown" SnapsToDevicePixels="True" MinWidth="{TemplateBinding ActualWidth}"
MaxHeight="{TemplateBinding MaxDropDownHeight}">
<Border x:Name="DropDownBorder" Background="White" BorderThickness="1"/>
<ScrollViewer Margin="4,6,4,6" SnapsToDevicePixels="True">
<StackPanel IsItemsHost="True" KeyboardNavigation.DirectionalNavigation="Contained" />
</ScrollViewer>
</Grid>
</Popup>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
</Style.Triggers>
</Style>
</UserControl.Resources>
用法:
<ComboBox ItemsSource="{Binding MtrCm}" SelectedValue="{Binding WellboreDiameter_Unit, Mode=TwoWay}" Grid.Row="1" Height="23" HorizontalAlignment="Right" Margin="0,26,249,0" x:Name="cboWellDiameter" VerticalAlignment="Top" Width="120" />
任何人都可以帮助我知道或设置错误/要添加以使其按要求工作,并在组合框中查看所选项目文本。非常感谢。
答案 0 :(得分:1)
您是否在WellboreDiameter_Unit上实施了propertychanged?
您也可以尝试使用SelectedItem吗?
编辑 - 抱歉再次看你的风格,我认为ContentPresenter的风格有问题。我将再看看,看看我是否可以提供帮助
编辑 - 您的ContentPresenter错误,请将其更改为
<ContentPresenter Name="ContentSite" IsHitTestVisible="False"
Margin="3,3,23,3" VerticalAlignment="Center" HorizontalAlignment="Left"
Content="{TemplateBinding SelectionBoxItem}"/>
确保添加Content =“{TemplateBinding SelectionBoxItem}”,您应该会看到selectedItem