Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/267.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将单选按钮绑定到枚举无效_C#_Wpf_Data Binding_Enums_Radio Button - Fatal编程技术网

C# 将单选按钮绑定到枚举无效

C# 将单选按钮绑定到枚举无效,c#,wpf,data-binding,enums,radio-button,C#,Wpf,Data Binding,Enums,Radio Button,我对WPF非常陌生,不了解绑定概念。我正在使用此命令将枚举绑定到单选按钮 该项目的总体思路是,我正在WPF中实现自己的TextEditor。我可以同时打开多个文本文件;我有一个主窗口,它有一个菜单项,用于选择DisplayForamt:Binary,Hex。我想将其绑定到File.cs中的属性 问题是:当打开两个文件并单击File1的十六进制时,File2的格式应保持为二进制。但是,两个文件的格式都会更改。我做错了什么 //In MainWindow.xaml: <MenuItem H

我对WPF非常陌生,不了解绑定概念。我正在使用此命令将枚举绑定到单选按钮

该项目的总体思路是,我正在WPF中实现自己的TextEditor。我可以同时打开多个文本文件;我有一个主窗口,它有一个菜单项,用于选择DisplayForamt:Binary,Hex。我想将其绑定到File.cs中的属性

问题是:当打开两个文件并单击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; } }
}
//在MainWindow.xaml中:
//在MainWindow.xaml.cs中:
公共类EnumToBooleanConverter:IValueConverter
{
公共对象转换(对象值、类型targetType、对象参数、System.Globalization.CultureInfo区域性)
{
返回值.Equals(参数);
}
公共对象转换回(对象值、类型targetType、对象参数、System.Globalization.CultureInfo区域性)
{
返回值.Equals(true)?参数:Binding.DoNothing;
}
}
//添加新选项卡时,这是代码
段落=新段落();
Add(System.IO.File.ReadAllText(filePath));
流程文件=新流程文件(段落);
mcRTB.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\u DataContentChanged);
tab.Title=ExtractFileName(文件路径);
tab.Content=mcRTB;
tab.Focus();
tab.DataContext=txtFile;
//InTextFile.cs
公共枚举显示格式{二进制,十六进制};
公共类文本文件:INotifyPropertyChanged
{
私有显示格式m_格式;
[默认值(displayFormat.Binary)]
public displayFormat displayFormat{get{return m_format;}set{m_format=value;}}
}

我已编辑了您的问题以提高清晰度,请检查以确保我没有犯任何错误。我所做的最重要的更改是实际链接到您正在使用的示例。仅仅说“我在StackOverflow上使用了答案”并不能真正帮助我们找到它:)。您是否为每个选项卡生成单独的视图模型(
TextFile
)?您需要使此功能正常工作。您认为每个选项卡都有一个单独的视图模型是什么意思?每个选项卡都有自己的
TextFile
实例吗?如果不是,它将解释为什么两个选项卡都更改为所选格式。您能否在调试器中验证它是否实际使用了两种不同的数据上下文?或者,这两个变量实际上都在设置?