I am pretty new to WPF and not aware of binding concepts. I am binding an enum to radio buttons using this answer.
The general idea of the project is that I am implementing my own TextEditor in WPF. I can open multiple TextFiles at the same time; I have a MainWindow, which has a MenuItem for selecting DisplayForamt: Binary, Hex. I want to bind this to a property in File.cs.
The problem is: When I have two files open and click HexaDecimal for File1, then the format for File2 should remain Binary. However the format changes for both the files. What am I doing wrong?
//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; } }
}