簡體   English   中英

WPF組合框所選項目不可見

[英]WPF Combobox Selected Item not visible

在我的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" />

誰能幫我知道或有什么設置錯誤/要添加此設置以使其能夠按要求使用,並在組合框中查看選定的項目文本。 非常感謝。

您是否正在WellboreDiameter_Unit上實現屬性更改?

您還可以嘗試使用SelectedItem嗎?

編輯-很抱歉再次查看您的樣式,我認為ContentPresenter的樣式有問題。 我會再看一次,看看是否可以幫忙

編輯-您的ContentPresenter錯誤,將其更改為以下內容

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

確保添加Content =“ {TemplateBinding SelectionBoxItem}”,並且應該看到selectedItem

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM