简体   繁体   English

WPF组合框所选项目不可见

[英]WPF Combobox Selected Item not visible

In my WPF app UserControl I want to set the background of the combo box. 在我的WPF应用UserControl中,我想设置组合框的背景。 After seeking lots of help and tutorials I could achieve it as expected, but now am not able to see the selected item in the text. 在寻求大量帮助和教程之后,我可以按预期实现,但现在无法在文本中看到所选的项目。 I mean, the selected item could be seen in drop down, but not in combo box when any drop is not clicked. 我的意思是,当未单击任何下拉菜单时,可以在下拉菜单中看到选定的项目,但是在下拉列表中看不到。 I tried many variations to set the fore color of the text, but no success. 我尝试了多种变体来设置文本的前景色,但没有成功。 Also tried thru different sites, but couldn't get everything workable. 还尝试通过不同的站点,但无法使所有事情都可行。 Am stuck badly since days on this. 自几天以来一直严重卡住。 Below is my code. 下面是我的代码。

    <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>

USAGE : 用法:

   <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" />

Can anyone help me know or what setting is wrong/to be added to make it workable as required and see the selected item text in the combo box. 谁能帮我知道或有什么设置错误/要添加此设置以使其能够按要求使用,并在组合框中查看选定的项目文本。 Thanks a lot. 非常感谢。

Are you implementing propertychanged on WellboreDiameter_Unit? 您是否正在WellboreDiameter_Unit上实现属性更改?

Also can you try using SelectedItem instead? 您还可以尝试使用SelectedItem吗?

Edit - Sorry looking at your style again, I think there is something wrong with the style for ContentPresenter. 编辑-很抱歉再次查看您的样式,我认为ContentPresenter的样式有问题。 I will have a look again and see if I can help 我会再看一次,看看是否可以帮忙

Edit - Your ContentPresenter is wrong, Change it to below 编辑-您的ContentPresenter错误,将其更改为以下内容

<ContentPresenter Name="ContentSite" IsHitTestVisible="False" 
Margin="3,3,23,3" VerticalAlignment="Center" HorizontalAlignment="Left" 
Content="{TemplateBinding SelectionBoxItem}"/>

Make sure you add the Content="{TemplateBinding SelectionBoxItem}" and you should see the selectedItem 确保添加Content =“ {TemplateBinding SelectionBoxItem}”,并且应该看到selectedItem

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM