在XAML中更改组合框的默认文本样式

时间:2014-01-13 13:13:36

标签: c# wpf xaml combobox

我正在尝试使用XAML创建一个组合框,这是一个下拉列表,默认文本已经在Italics的框中,当您单击下拉列表以展开选项列表时,所有选项将以普通文本而非斜体列出。进行选择时,我希望所选选项仍然是正常的而不是斜体,即使它与默认文本位于同一位置。我是XAML的新手,我不知道如何做到这一点,或者甚至是否可能?

我的组合框现在是,如下所示,要显示的默认文本位于属性“文本”中。基本上,我希望“默认文本”是斜体,但没有别的。

<ComboBox x:Name="ColumnComboBox" Grid.Column="1" Width="200" Margin="0,2"  IsEditable="True" Text="Default Text" FontWeight="Normal"  />

非常感谢任何帮助。

2 个答案:

答案 0 :(得分:1)

你需要做更多的工作才能实现这一目标 试试这个

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded">        
    <Grid>
        <ComboBox Name="comboBox1" Margin="40,55,192,225" FontStyle="Italic">
            <ComboBox.ItemTemplate>
                <DataTemplate>
                    <Label Content="{Binding}" ></Label>
                </DataTemplate>
            </ComboBox.ItemTemplate>
            <ComboBox.ItemContainerStyle>
                <Style TargetType="{x:Type ComboBoxItem}">
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="{x:Type ComboBoxItem}">
                                <Label   Name="lbl" Content="{Binding}" ></Label>
                                <ControlTemplate.Triggers>
                                    <Trigger Property="IsHighlighted" Value="True">
                                        <Setter TargetName="lbl" Property="FontStyle" Value="Normal"> </Setter>
                                        <Setter TargetName="lbl" Property="Background" Value="AliceBlue"></Setter>
                                    </Trigger>
                                    <Trigger Property="IsSelected" Value="True">
                                        <Setter TargetName="lbl" Property="FontStyle" Value="Italic"></Setter>
                                    </Trigger>
                                    <Trigger Property="IsSelected" Value="False">
                                        <Setter TargetName="lbl" Property="FontStyle" Value="Normal"></Setter>
                                    </Trigger>
                                </ControlTemplate.Triggers>
                            </ControlTemplate>

                        </Setter.Value>
                    </Setter>
                </Style>
            </ComboBox.ItemContainerStyle>

        </ComboBox>




    </Grid>
</Window>

此处用于测试代码背后的样式

using System.Collections.ObjectModel;
using System.Windows;

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window

    {
        public  ObservableCollection<string>  observableCollection  =  new ObservableCollection<string>();
        public MainWindow()
        {
            for (int i = 0; i < 10; i++)
            {
                observableCollection.Add( "item:"+ i.ToString());
            }
            InitializeComponent();
            comboBox1.ItemsSource = observableCollection;  
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {

        }
    }
}

答案 1 :(得分:0)

有可能,但正如您已经注意到的那样,默认文本和选择项目时显示的文本是相同的文本。这就是为什么将默认文本的样式设置为与所选项目的文本样式不同会很棘手。最简单的实现是收听ComboBox's选择更改事件。选择项目后,将ComboBox's FontStyle更改为“正常”,如果未选择任何项目,则将其更改为斜体

<ComboBox x:Name="ColumnComboBox" SelectionChanged="ColumnComboBox_SelectionChanged" IsEditable="True" Text="Default Text" FontWeight="Italic">
    <ComboBoxItem Content="Item 1" FontStyle="Normal"/>
</ComboBox>

private void ColumnComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    ColumnComboBox.FontStyle = ColumnComboBox.SelectedItem != null ? FontStyles.Normal : FontStyles.Italic;
}

或许你真的想要ComboBox的水印行为。检查blog post关于水印ComboBox 的可靠实现,可下载的源代码here