C# 使用触发器替换单个组合框项上的文本值
我正在使用ComboBox控件设置物理设备的地址整数值。ComboBox ItemSource绑定到整数列表,其中第一个值'-1'表示未连接任何设备 我希望“-1”值显示为“无”。是否可以使用触发器来完成此操作,以便替换下拉列表中的文本和组合框中的文本(如果已选择)?我正在考虑使用enum和Description属性,但我希望我不必这样做 例如: XAML:C# 使用触发器替换单个组合框项上的文本值,c#,wpf,combobox,C#,Wpf,Combobox,我正在使用ComboBox控件设置物理设备的地址整数值。ComboBox ItemSource绑定到整数列表,其中第一个值'-1'表示未连接任何设备 我希望“-1”值显示为“无”。是否可以使用触发器来完成此操作,以便替换下拉列表中的文本和组合框中的文本(如果已选择)?我正在考虑使用enum和Description属性,但我希望我不必这样做 例如: XAML: 代码: 公共类MainWindowVM:INotifyPropertyChanged { 公共事件属性更改事件处理程序属性更改; 公
代码:
公共类MainWindowVM:INotifyPropertyChanged
{
公共事件属性更改事件处理程序属性更改;
公共列表地址选择
{
获取{return new List(){-1,1,2,3,4};}
}
专用int_选择的地址;
公共整数选择地址
{
获取{return\u selectedAddress;}
设置
{
SetNotify(参考所选地址、值);
WriteLine(“所选地址为{0}”,值);
}
}
public void SetNotify(ref T storage,T value,[CallerMemberName]string propertyName=null)
{
储存=价值;
PropertyChanged?.Invoke(这是新的PropertyChangedEventArgs(propertyName));
}
}
使用组合框ItemTemplate中的ValueConverter为我解决了这个问题
XAML:
简短回答:是的。您应该能够在纯XAML中执行此操作:
<ComboBox Height="30"
ItemsSource="{Binding Path=AddressSelection}"
SelectedItem="{Binding Path=SelectedAddress}">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock>
<TextBlock.Style>
<Style TargetType="TextBlock">
<Setter Property="Text" Value="{Binding}" />
<Style.Triggers>
<DataTrigger Binding="{Binding}" Value="-1">
<Setter Property="Text" Value="NONE" />
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
@Oystein如果您将列表/集合绑定到组合框,为什么不直接将所需数据放入其中?这有什么限制吗?不错。我想我会支持你的方法,避免使用转换器。感谢您提供了一个漂亮的解决方案。:-)
public class MainWindowVM : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public List<int> AddressSelection
{
get { return new List<int>() { -1, 1, 2, 3, 4 }; }
}
private int _selectedAddress;
public int SelectedAddress
{
get { return _selectedAddress; }
set
{
SetNotify(ref _selectedAddress, value);
Console.WriteLine("Selected address is {0}", value);
}
}
public void SetNotify<T>(ref T storage, T value, [CallerMemberName] string propertyName = null)
{
storage = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
<Window.Resources>
<local:ValueConverter x:Key="ValueConverter" />
</Window.Resources>
<ComboBox Height="30"
ItemsSource="{Binding Path=AddressSelection}"
SelectedItem="{Binding Path=SelectedAddress}">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding ., Converter={StaticResource ValueConverter}}" />
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
public class ValueConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if ((int)value != -1)
{
return ((int)value).ToString();
}
else
{
return "NONE";
}
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
<ComboBox Height="30"
ItemsSource="{Binding Path=AddressSelection}"
SelectedItem="{Binding Path=SelectedAddress}">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock>
<TextBlock.Style>
<Style TargetType="TextBlock">
<Setter Property="Text" Value="{Binding}" />
<Style.Triggers>
<DataTrigger Binding="{Binding}" Value="-1">
<Setter Property="Text" Value="NONE" />
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>