c# - 在 WPF 中为组合框设置默认名称

标签 c# wpf xaml

我有一个组合框,里面装满了代表年份的整数。多年来我将它们添加到 ObservableCollection,但我的问题是当我加载项目时组合框默认为空白。我想为其设置一个默认名称,如“年”,但我不希望将 isEditable 设置为 true 或在开头插入字符串等解决方案。如果可能的话,我想要一个纯 xaml 解决方案。

这是我当前的 xaml 文件:

<RSControls:SmoothScrollComboBox Grid.Column="1" x:Name="compilationYearCombo" Margin="7,2.04,0,2.04"                                                                                     
                            SelectedValue="{Binding Path=SelectedYear}"
                            SelectedValuePath=""
                            ItemsSource="{Binding Years}"
                            DisplayMemberPath="" SelectionChanged="compilationYearCombo_SelectionChanged" IsSynchronizedWithCurrentItem="True" Grid.ColumnSpan="2" IsEditable="False" SelectedIndex="0" IsReadOnly="False" Text="Years">

                        </RSControls:SmoothScrollComboBox>

我尝试添加 <TextBlock Text="Years" /> ,但这只会将组合中的所有元素更改为“年”。<​​/p>

我很欣赏详细的说明,我只是 WPF 的初学者。

谢谢。

最佳答案

您可以将可见性转换器添加到您的 TextBlock

   <TextBlock
           Visibility="{Binding SelectedItem, ElementName=compilationYearCombo, Converter={StaticResource NullToVisibilityConverter}}"
           IsHitTestVisible="False"
           Text="Years" />

使用这个转换器:

    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
}

关于c# - 在 WPF 中为组合框设置默认名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7202175/

相关文章:

c# - FluentValidation:检查两个字段之一是否为空

wpf - 如何更改 ControlTemplate 中标签的字体大小

wpf - MultiDataTrigger 与 AlternationIndex

c# - 在 WP8 上更改按钮边框颜色时滞后

c# - 在运行时选择性地禁用 ServiceStack 中的某些 REST 服务

c# - 如何确定字符串是否是已安装 UWP 应用程序的 AUMID?

c# - 哪个称为第一个静态构造函数或私有(private)构造函数

c# - 哪个 Eyeshot 实体效率更高?

C#:HttpClient,服务器违反了协议(protocol)。部分=响应状态行

c# - 试图让 Windows Phone 8 ListPicker 工作