How to manually trigger the converter method for property of ComboBox in WPF
- Dominant language
- C#
- Stars
- 7.7k
- Forks
- 1.3k
- Avg merge
- 1d 11h
- Merged PRs (30d)
- 61
Description
_This issue has been moved from [a ticket on Developer Community](https://developercommunity.visualstudio.com/t/How-to-manually-trigger-the-converter-me/10662342)._
---
[severity:It bothers me. A fix would be nice]
```
public enum AlarmLevel
{
[Description("正常")]
Normal = 0,
[Description("警报")]
Warring = 1,
[Description("故障")]
Error = 2,
}
public partial class MainWindow : Window, INotifyPropertyChanged
{
public MainWindow()
{
InitializeComponent();
DataContext = this;
InitList();
}
public List AlarmLevelList
{
get
{
return _alarmLevelList;
}
set
{
_alarmLevelList = value;
DoPropertyChanged(AlarmLevelListPropertyName);
}
}
private List _alarmLevelList;
public const string AlarmLevelListPropertyName = "AlarmLevelList";
public AlarmLevel SelectedAlarmLevel
{
get { return _selectedAlarmLevel; }
set
{
if (_selectedAlarmLevel != value)
{
_selectedAlarmLevel = value;
DoPropertyChanged(SelectedAlarmLevelPropertyName);
}
}
}
private AlarmLevel _selectedAlarmLevel;
public const string SelectedAlarmLevelPropertyName = "SelectedAlarmLevel";
public void InitList()
{
AlarmLevelList = new List();
AlarmLevelList.Add(AlarmLevel.Normal);
AlarmLevelList.Add(AlarmLevel.Warring);
AlarmLevelList.Add(AlarmLevel.Error);
}
private void Button_Click(object sender, RoutedEventArgs e)
{
DoPropertyChanged(AlarmLevelListPropertyName);
DoPropertyChanged(SelectedAlarmLevelPropertyName);
}
public event PropertyChangedEventHandler PropertyChanged;
protected void DoPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
public class EnumDescriptionConverter : IValueConverter
{
object IValueConverter.Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value != null && value as string != "")
{
Enum myEnum = (Enum)value;
string description = GetEnumDescription(myEnum);
return description;
}
return "";
}
object IValueConverter.ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return string. Empty;
}
private string GetEnumDescription(Enum enumObj)
{
FieldInfo fieldInfo = enumObj.GetType(). GetField(enumObj.ToString());
object[] attribArray = fieldInfo.GetCustomAttributes(false);
if (attribArray.Length == 0)
{
return enumObj.ToString();
}
else
{`
DescriptionAttribute attrib = attribArray[0] as DescriptionAttribute;
return attrib. Description;
}
}
}
```
There are two controls on UI, one is ComboBox, and the other is button, when user press Button, how to trigger converter enumDescriptionConverter of ComboBox control manually in Button_Click event?
Only after application is launched for first time, the converter enumDescriptionConverter can be executed. So I have tried writing following code within Button Click method, and it works as expected (the converter was triggered four times, three times for ItemSource, and one time for SelectedItem). But I think it is not a good style of writing code. Could anyone give good suggestion than this one? Thanks in advance!
```
private void Button_Click(object sender, RoutedEventArgs e)
{
List tempList = AlarmLevelList ;
AlarmLevelList = null;
AlarmLevelList = tempList;
DoPropertyChanged(SelectedAlarmLevelPropertyName);
}
```
---
### Original Comments
#### Feedback Bot on 5/21/2024, 10:54 AM:
(private comment, text removed)
---
### Original Solutions
(no solutions)
Contributor guide
Assessment
This issue has not been assessed yet.