WPF AvalonEdit不显示ViewModel中的绑定TextDocument

时间:2014-06-13 21:54:57

标签: c# wpf mvvm avalonedit

我的输出窗口中没有Binding错误。

我想通过MVVM将文件中的文本绑定到一个/多个avalonEdit控件。

我怎么能这样做,因为它不按我试用的方式工作?!

查看     

        >
    <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>

视图模型

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");
            }
        }
    }

我看过那个样本是MVVM引入的:

http://www.codeproject.com/Articles/570313/AvalonDock-Tutorial-Part-AvalonEdit-in-Avalo

我在加载文本文件时实际执行相同的绑定但是我的文本没有显示:/

你可以在这里找到一个repro visual studio解决方案:

http://www.file-upload.net/download-9067428/ExpanderTest.7z.html

0 个答案:

没有答案