我有一个组合框,里面装满了代表年份的整数。多年来我将它们添加到 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/