C# WPF AvaloneEdit不显示来自ViewModel的绑定文本文档
我的输出窗口中没有绑定错误 我想通过MVVM将文件中的文本绑定到一个/多个AvaloneEdit控件 我怎么能这么做,因为它没有像我尝试的那样工作 查看C# WPF AvaloneEdit不显示来自ViewModel的绑定文本文档,c#,wpf,mvvm,avalonedit,C#,Wpf,Mvvm,Avalonedit,我的输出窗口中没有绑定错误 我想通过MVVM将文件中的文本绑定到一个/多个AvaloneEdit控件 我怎么能这么做,因为它没有像我尝试的那样工作 查看 > <Window.Resources> <DataTemplate x:Key="NormalTemplate"> <Expander Margin="0" Header="{Binding FileName}">
>
<Window.Resources>
<DataTemplate x:Key="NormalTemplate">
<Expander Margin="0" Header="{Binding FileName}">
<!--<TextBox TextWrapping="Wrap" AcceptsReturn="True" IsReadOnly="True" Text="{Binding Content}" Margin="0"/>-->
<avalonEdit:TextEditor Document="{Binding Document, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"></avalonEdit:TextEditor>
</Expander>
</DataTemplate>
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="40px"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<Grid Background="Orange">
<StackPanel Orientation="Horizontal">
<Button>Run</Button>
<Button Command="{Binding SaveCommand}">Save</Button>
</StackPanel>
</Grid>
<Grid Grid.Row="1">
<ScrollViewer ScrollViewer.VerticalScrollBarVisibility="Auto" ScrollViewer.HorizontalScrollBarVisibility="Disabled">
<ItemsControl ItemsSource="{Binding ErrorLogs}" HorizontalContentAlignment="Stretch" ItemTemplate="{DynamicResource NormalTemplate}" />
</ScrollViewer>
</Grid>
</Grid>
</Window>
>
实际上,我在加载文本文件时也会执行相同的绑定,但我的文本不会显示:/
您可以在此处找到Repo visual studio解决方案:
我发现,只有当我将AvaloneEdit放入ItemsControl中时,才会出现问题,然后不会显示任何文本。为什么会这样?我在这里打开了一个bug问题:因为绑定到ItemsControl中的文本框很好!
public class ErrorLogViewModel : BaseViewModel
{
public string FileName { get; set; }
public string Content { get; set; }
private TextDocument _document = null;
public TextDocument Document
{
get { return this._document; }
set
{
if (this._document != value)
{
this._document = value;
RaisePropertyChanged("Document");
}
}
}
}
public class MainViewModel : BaseViewModel
{
public MainViewModel()
{
GetErrorLogs();
SaveCommand = new RelayCommand(Save);
}
public RelayCommand SaveCommand { get; private set; }
private void Save()
{
var text = ErrorLogs[0].Document.Text;
// I get at least the Original loaded text
}
private void GetErrorLogs()
{
for (int i = 0; i < 30; i++)
{
using (FileStream fs = new FileStream("text.txt", FileMode.Open, FileAccess.Read, FileShare.Read))
{
using (StreamReader reader = FileReader.OpenStream(fs, Encoding.UTF8))
{
var doc = new TextDocument(reader.ReadToEnd());
var e = new ErrorLogViewModel { FileName = "ErrorLog " + i, Document = doc };
ErrorLogs.Add(e);
}
}
}
}
private ObservableCollection<ErrorLogViewModel> _errorLogs = new ObservableCollection<ErrorLogViewModel>();
public ObservableCollection<ErrorLogViewModel> ErrorLogs
{
get { return _errorLogs; }
set
{
_errorLogs = value;
this.RaisePropertyChanged("ErrorLogs");
}
}
}