0

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; } }
}
Community
  • 1
  • 1
user2495173
  • 311
  • 2
  • 5
  • 17
  • I have edited your question to improve clarity, please review it to make sure I didn't make any mistakes. The most important change I made was to actually link to the example you are using. Just saying "I used an answer on StackOverflow" doesn't really help us find it :). – BradleyDotNET May 15 '14 at 00:33
  • Are you generating a separate view model (`TextFile`) for each tab? You'll need to to make this work. – BradleyDotNET May 15 '14 at 00:37
  • What do you mean by a separate view model for each tab? – user2495173 May 15 '14 at 00:40
  • 1
    Does each tab have its own instance of `TextFile`? If not, it would explain why both tabs are changing to the selected format. – BradleyDotNET May 15 '14 at 00:41
  • Yes each Tab has its own instance of TextFile. But when I set the DisplayFormat for one file, property changes for all the files. – user2495173 May 15 '14 at 00:43
  • 1
    Can you verify in the debugger that it is actually *using* two different data contexts? Or alternatively that both variables are actually being set? – BradleyDotNET May 15 '14 at 00:49
  • Yes when I debug, I do see that DisplayFormat is different for two files, but by the time it diplays in the radio button, it changes. I need to find where this is getting reset. – user2495173 May 15 '14 at 00:53
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/52716/discussion-between-user2495173-and-bradleydotnet) – user2495173 May 15 '14 at 01:23

0 Answers0