【问题标题】:Textbox does not linebreak within expander control文本框不会在扩展器控件中换行
【发布时间】:2014-06-13 20:15:34
【问题描述】:

我有一个 ItemsControl,里面有很多 Expander,里面有一个 TextBox。

TextBox 有很长的 Text,但它的文本没有换行,而是我在 ItemsControl 上看到了一个我不想要的水平滚动条。我想要文本框上的水平滚动条。我不想要的事件,而是 Textobx 中的文本应该始终环绕并具有垂直滚动条。

我该怎么做?

<Window x:Class="ExpanderTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="768" Width="1024">
    <Window.Resources>
        <DataTemplate x:Key="NormalTemplate">
            <Expander Margin="0" Header="{Binding FileName}" Background="Green">
                <TextBox TextWrapping="Wrap" AcceptsReturn="True"  IsReadOnly="True" Text="{Binding Content}"  Margin="0"/>
            </Expander>
        </DataTemplate>
    </Window.Resources>

    <Grid>
        <ListBox ItemsSource="{Binding ErrorLogs}" HorizontalContentAlignment="Stretch" ItemTemplate="{DynamicResource NormalTemplate}" />
    </Grid>
</Window>


public class MainViewModel : BaseViewModel
    {
        public MainViewModel()
        {
            GetErrorLogs();
        }

        private void GetErrorLogs()
        {
            for (int i = 0; i < 30; i++)
            {
                string content = new string('0', 500 * i + 1);
                var e = new ErrorLogViewModel { Content = content, FileName = "ErrorLog " + i };
                ErrorLogs.Add(e);
            }

        }

        private ObservableCollection<ErrorLogViewModel> _errorLogs = new ObservableCollection<ErrorLogViewModel>();
        public ObservableCollection<ErrorLogViewModel> ErrorLogs
        {
            get { return _errorLogs; }
            set
            {
                _errorLogs = value;
                this.RaisePropertyChanged("ErrorLogs");
            }
        }
    }

【问题讨论】:

    标签: c# wpf


    【解决方案1】:

    ListBox 的默认模板包含一个 ScrollViewer 控件来展示您的项目。此 ScrollViewer 显示滚动条。

    要禁用水平滚动条,请在ListBox 上设置以下属性:

    <ListBox ScrollViewer.HorizontalScrollBarVisibility="Disabled" ...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-01-22
      • 2016-08-09
      • 1970-01-01
      • 2012-05-14
      • 1970-01-01
      • 1970-01-01
      • 2012-06-29
      相关资源
      最近更新 更多