简体   繁体   中英

C# WPF - Disabled ComboBox should look like label

I'm trying to change the style of my combobox in wpf when it's disabled. It should look like a plain text (label).

Here is my code:

<Style TargetType="{x:Type ComboBox}">
    <Style.Triggers>
        <Trigger Property="IsEnabled" Value="false">
            <Setter Property="Foreground" Value="Black" />
            <Setter Property="BorderBrush" Value="Black" />
            <Setter Property="BorderThickness" Value="0"></Setter>
            <Setter Property="Background" Value="{x:Null}" />

        </Trigger>
    </Style.Triggers>
</Style>

but it doesnt seem to work. any hints?

You can add ControlTemplate , so the combobox will shown like a TextBox(without border, background, toggle button, ect.). but it act as a combobx(having drop-down list). Drop-down will not shown if the control is disbled(hence it will shown like a label)

<ComboBox.Template>
<ControlTemplate>
    <TextBlock Text="{Binding SelectedItem.MyText,RelativeSource={RelativeSource Mode=TemplatedParent}}"></TextBlock>
</ControlTemplate>
</ComboBox.Template>

If the control has a tendency to look like another control on certain condition, this kind of requirement can be satisfied by ContentControl. You can switch to appropriate content based on a given condition.

<ContentControl IsEnabled="True" /> // or IsEnabled="False"

Then switch via Style..

<Style TargetType="{x:Type ContentControl}">
    <Style.Triggers>
            <Trigger Property="IsEnabled" Value="true">
                // combobox
            </Trigger>
            <Trigger Property="IsEnabled" Value="false">
                 // Label
            </Trigger>
    </Style.Triggers>
</Style>

Just place two controls, the ComboBox and the Label. Bind the Visibility property of each to your boolean indicating if the ComboBox should be enabled so that one is visible when enabled and the other when disabled.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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