WPF 中组合框的名称
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11898222/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Name on combobox in WPF
提问by 3D-kreativ
When I create a combobox in WPF I want it to have a name on it like "Your choice", just like if it was a common button and when I click on it I want only the items to dropdown, not the name on the combobox again. I hope you understand my question? Is there a way to solve this?
当我在 WPF 中创建一个组合框时,我希望它有一个名称,例如“您的选择”,就像它是一个通用按钮一样,当我单击它时,我只希望下拉项目,而不是组合框上的名称再次。我希望你明白我的问题?有没有办法解决这个问题?
In the XAML I use this for the combobox:
在 XAML 中,我将其用于组合框:
<ComboBox Height="23" HorizontalAlignment="Left" Margin="110,226,0,0" Name="cmbChangeRoute" VerticalAlignment="Top" Width="156" SelectionChanged="cmbChangeRoute_SelectionChanged" />
And I add items in the C# code like this:
我在 C# 代码中添加项目,如下所示:
string[] strChangeRoute = new string[] { "Your choice", "10 deg", "20 deg", "30 deg" };
foreach (string s in strChangeRoute)
cmbChangeRoute.Items.Add(s);
cmbChangeRoute.SelectedIndex = 0;
回答by franssu
I would go for a TextBlock over the ComboBox who's visibility would be bound to the selectedItem of the ComboBox (through a converter).
我会在 ComboBox 上寻找一个 TextBlock,它的可见性将绑定到 ComboBox 的 selectedItem(通过转换器)。
<Grid>
<ComboBox x:Name="myComboBox" />
<TextBlock Text="Your choice.."
IsHitTestVisible="False"
Visibility="{Binding ElementName=myComboBox, Path=SelectedItem,
Converter={StaticResource yourChoiceLabelVisibilityConverter}}"/>
</Grid>
public class YourChoiceLabelVisibilityConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter,
System.Globalization.CultureInfo culture)
{
if (value == null)
{
return Visibility.Visible;
}
return Visibility.Hidden;
}
OR, a better solution : pure xaml, using triggers :
或者,更好的解决方案:纯 xaml,使用触发器:
<ContentControl x:Name="myContentControl" Content="{Binding}">
<ContentControl.ContentTemplate>
<DataTemplate>
<Grid>
<ComboBox x:Name="myComboBox" ItemsSource="{Binding}"/>
<TextBlock x:Name="myTextBlock"
Text="Your choice.."
IsHitTestVisible="False"
Visibility="Hidden"/>
</Grid>
<DataTemplate.Triggers>
<Trigger SourceName="myComboBox" Property="SelectedItem"
Value="{x:Null}">
<Setter TargetName="myTextBlock" Property="Visibility
Value="Visible"/>
</Trigger>
</DataTemplate.Triggers>
</DataTemplate>
</ContentControl.ContentTemplate>
</ContentControl>
In this case don't forget to set your content control's datacontext from the codebehind :
在这种情况下,不要忘记从代码隐藏设置内容控件的数据上下文:
myContentControl.DataContext = Enum.GetValues(typeof([YOUR ENUM]));
回答by Clinton Ward
Try this, I just tested it
试试这个,我刚刚测试过
<ComboBox Height="23" HorizontalAlignment="Left" Margin="110,226,0,0" Name="cmbChangeRoute" VerticalAlignment="Top" Width="156" IsManipulationEnabled="False" IsEditable="True" Text="Your Choice..." SelectionChanged="cmbChangeRoute_SelectionChanged">
回答by Ivan Petkov
Have you tried using binding?
您是否尝试过使用绑定?
In XAML you have something like
在 XAML 中,你有类似的东西
<ComboBox ... SelectedItem="{Binding ChosenValue,Mode=TwoWay}" ... />
Then, in your constructor (in the code-behind) just add the line
然后,在您的构造函数中(在代码隐藏中)只需添加行
this.DataContext = this;
So that your binding would actually look in the code-behind to find the dependency property ChosenValue. This way, every time the value in the combobox is changed, your prop's value would update to hold the currently selected item.
这样您的绑定实际上会在代码隐藏中查找依赖属性 ChosenValue。这样,每次组合框中的值更改时,道具的值都会更新以保存当前选定的项目。
To achieve what you want, just set the prop's value to "Your Choice" in the constructor
要实现您想要的,只需在构造函数中将道具的值设置为“您的选择”
public ClassName()
{
InitializeComponent();
this.DataContext = this;
ChosenValue = "Your Choice";
}
Similarly, just set its value to the same string everywhere else you want. When saving or whatever, just check
同样,只需将其值设置为您想要的其他任何地方的相同字符串。保存或其他时,只需检查
if(!ChosenValue.Equals("Your Choice")
{
//do logic
}
else
{
//the user has not selected anything
}
Hope this helps!
希望这可以帮助!
回答by quetzalcoatl
What you want to do is actually not to configure the ComboBox, but to add an adorner/decorator over it, that would display a text while the Combo is closed, and that would hide itself when the combo is down. It sometimes is called "watermarking".
你想要做的实际上不是配置 ComboBox,而是在它上面添加一个装饰器/装饰器,它会在 Combo 关闭时显示一个文本,当组合关闭时它会隐藏自己。它有时被称为“水印”。
I will not explain it further, because it is pointless. Here's a nice article: http://pwlodek.blogspot.com/2009/11/watermark-effect-for-wpfs-textbox.htmlthere's also all the code snippest required for watermarking the comboboxes.
我不会进一步解释它,因为它毫无意义。这是一篇不错的文章:http: //pwlodek.blogspot.com/2009/11/watermark-effect-for-wpfs-textbox.html 还有对组合框加水印所需的所有代码片段。
回答by Sivakumar
You can refer this one, How to display default text "--Select Team --" in combo box on pageload in WPF?
您可以参考这个, 如何在 WPF 页面加载的组合框中显示默认文本“--Select Team --”?
I would suggest the below solution from this forum,
我会建议这个论坛的以下解决方案,
you can do this without any code behind by using a IValueConverter.
您可以使用 IValueConverter 在没有任何代码的情况下执行此操作。
<Grid>
<ComboBox
x:Name="comboBox1"
ItemsSource="{Binding MyItemSource}" />
<TextBlock
Visibility="{Binding SelectedItem, ElementName=comboBox1, Converter={StaticResource NullToVisibilityConverter}}"
IsHitTestVisible="False"
Text="... Select Team ..." />
</Grid>
Here you have the converter class that you can re-use.
这里有可以重用的转换器类。
public class NullToVisibilityConverter : IValueConverter
{
#region Implementation of IValueConverter
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return value == null ? Visibility.Visible : Visibility.Collapsed;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
#endregion
}
And finally, you need to declare your converter in a resource section.
最后,您需要在资源部分声明您的转换器。
<Converters:NullToVisibilityConverter x:Key="NullToVisibilityConverter" />
Where Converters is the place you have placed the converter class. An example is:
其中 Converters 是您放置转换器类的地方。一个例子是:
xmlns:Converters="clr-namespace:MyProject.Resources.Converters"
The very nice thing about this approach is no repetition of code in your code behind.
这种方法的好处是在你的代码后面没有重复的代码。

