我是WPF的新手,并不了解绑定概念。我使用此answer将枚举绑定到单选按钮。
该项目的一般概念是我在WPF中实现自己的TextEditor。我可以同时打开多个TextFiles;我有一个MainWindow,它有一个MenuItem用于选择DisplayForamt:Binary,Hex。我想将它绑定到File.cs中的属性。
问题是:当我打开两个文件并单击HexaDecimal for File1时,File2的格式应保持二进制。但是,两个文件的格式都会更改。我究竟做错了什么?
//In MainWindow.xaml:
<MenuItem Header="_DisplayFormat">
<RadioButton Content="_Binary" IsChecked="{Binding Path=DisplayFormat, Converter={StaticResource EnumToBooleanConverter}, ConverterParameter={x:Static local:displayFormat.Binary}}" />
<RadioButton Content="_Hexadecimal" IsChecked="{Binding Path=DisplayFormat, Converter={StaticResource EnumToBooleanConverter}, ConverterParameter={x:Static local:displayFormat.HexaDecimal}}" />
</MenuItem>
//In MainWindow.xaml.cs:
public class EnumToBooleanConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return value.Equals(parameter);
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return value.Equals(true) ? parameter : Binding.DoNothing;
}
}
//When a new tab is added this is the code
Paragraph paragraph = new Paragraph();
paragraph.Inlines.Add(System.IO.File.ReadAllText(filePath));
FlowDocument document = new FlowDocument(paragraph);
mcRTB.Document = document;
TextFile txtFile = new SegmentFile { Path = filePath, DataContent = document.ToString() };
txtFileArray [EditorTabcontrol.SelectedIndex+1] = txtFile ;
this.DataContext = txtFile ;
mcRTB.TextChanged += new System.Windows.Controls.TextChangedEventHandler(TextFile_DataContentChanged);
tab.Title = ExtractFileName(filePath);
tab.Content = mcRTB;
tab.Focus();
tab.DataContext = txtFile ;
//InTextFile.cs
public enum displayFormat { Binary, HexaDecimal};
public class TextFile : INotifyPropertyChanged
{
private displayFormat m_format;
[DefaultValue(displayFormat.Binary)]
public displayFormat DisplayFormat { get { return m_format; } set { m_format = value; } }
}