wpf 将RichTextBox的宽度绑定到模板中的网格

pokxtpni  于 2023-06-24  发布在  其他
关注(0)|答案(1)|浏览(113)

我有下面的主题模板为ListViewItems,但里面的RichTextBox垂直显示文本。如何编辑RichTextBox,使文本水平显示?

<Style TargetType="ListViewItem" x:Key="InformationListItemTheme">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate>
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="100"/>
                        <ColumnDefinition Width="*"/>
                        <ColumnDefinition Width="175"/>
                    </Grid.ColumnDefinitions>
                    <TextBlock Grid.Column="0"/>
                    <RichTextBox Grid.Column="1" Height="275"/>
                    <Grid Grid.Column="2">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition/>
                            <ColumnDefinition/>
                        </Grid.ColumnDefinitions>
                        <Grid.RowDefinitions>
                            <RowDefinition/>
                            <RowDefinition/>
                            <RowDefinition/>
                            <RowDefinition/>
                            <RowDefinition/>
                        </Grid.RowDefinitions>
                        <TextBlock Grid.Column="0" Grid.Row="0" Text="JA"/>
                        <TextBlock Grid.Column="0" Grid.Row="1" Text="SCH"/>
                        <TextBlock Grid.Column="0" Grid.Row="2" Text="ELT"/>
                        <TextBlock Grid.Column="0" Grid.Row="3" Text="KIN"/>
                        <TextBlock  Grid.Column="0" Grid.Row="4" Text="SON"/>
                        <TextBox Grid.Column="1" Grid.Row="0" Text="{Binding JA}"/>
                        <TextBox Grid.Column="1" Grid.Row="1" Text="{Binding SCH}"/>
                        <TextBox Grid.Column="1" Grid.Row="2" Text="{Binding ELT}"/>
                        <TextBox Grid.Column="1" Grid.Row="3" Text="{Binding KIN}"/>
                        <TextBox Grid.Column="1" Grid.Row="4" Text="{Binding SON}"/>
                    </Grid>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
ggazkfy8

ggazkfy81#

我做了一个可重复的例子。

    • 带ListView的User-Control和ListViewItems的模板:**
<UserControl x:Class="Protocol.MVVM.View.TestView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:viewModel="clr-namespace:Protocol.MVVM.ViewModel"
             xmlns:usc="clr-namespace:Protocol.UserControls" 
             xmlns:local="clr-namespace:Protocol.MVVM.View"
             mc:Ignorable="d" 
             d:DesignHeight="450" d:DesignWidth="500">

    <UserControl.DataContext>
        <viewModel:InformationsViewModel/>
    </UserControl.DataContext>

    <Grid VerticalAlignment="Stretch" HorizontalAlignment="Stretch">
        <Grid.RowDefinitions>
            <RowDefinition Height="50"/>
            <RowDefinition Height="50"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <TextBlock Text="Information"
                       Grid.Row="0"
                   FontSize="34"
                   FontWeight="DemiBold"
                   HorizontalAlignment="Center"/>

        <Grid Grid.Row="1">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="100"/>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="150"/>
            </Grid.ColumnDefinitions>

            <TextBlock Text="Name"
                   Grid.Column="0"
                   FontSize="12"
                   FontWeight="DemiBold"
                   HorizontalAlignment="Center"/>
            <TextBlock Text="Information"
                   Grid.Column="1"
                   FontSize="12"
                   FontWeight="DemiBold"
                   HorizontalAlignment="Center"/>
            <TextBlock Text="Docomentation"
                   Grid.Column="2"
                   FontSize="12"
                   FontWeight="DemiBold"
                   HorizontalAlignment="Center"/>
        </Grid>

        <Grid Grid.Row="2" HorizontalAlignment="Stretch">
            <ListView ItemsSource="{Binding Informations}" HorizontalAlignment="Stretch" Name="ListViewInformation"
                      Margin="2"
                      Grid.Row="2"
                      ScrollViewer.VerticalScrollBarVisibility="Visible"
                      BorderThickness="0"
                      Background="Transparent"
                      ScrollViewer.CanContentScroll="false">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <Grid Margin="4" MinHeight="25" HorizontalAlignment="Stretch">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="100"/>
                                <ColumnDefinition Width="*"/>
                                <ColumnDefinition Width="175"/>
                            </Grid.ColumnDefinitions>
                            <TextBlock Grid.Column="0" Text="{Binding Name}" Margin="4,0,0,0" VerticalAlignment="Top"/>

                            <RichTextBox Grid.Column="1" Height="275" VerticalAlignment="Center" HorizontalAlignment="Stretch">
                            </RichTextBox>

                            <Grid Grid.Column="2">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition/>
                                    <ColumnDefinition/>
                                </Grid.ColumnDefinitions>
                                <Grid.RowDefinitions>
                                    <RowDefinition/>
                                    <RowDefinition/>
                                    <RowDefinition/>
                                    <RowDefinition/>
                                    <RowDefinition/>
                                </Grid.RowDefinitions>
                                <TextBlock HorizontalAlignment="Stretch" Grid.Column="0" Grid.Row="0" Text="JUG" Margin="4,0,0,0" VerticalAlignment="Center"/>
                                <TextBlock HorizontalAlignment="Stretch" Grid.Column="0" Grid.Row="1" Text="SCH" Margin="4,0,0,0" VerticalAlignment="Center"/>
                                <TextBlock HorizontalAlignment="Stretch" Grid.Column="0" Grid.Row="2" Text="ELT" Margin="4,0,0,0" VerticalAlignment="Center"/>
                                <TextBlock HorizontalAlignment="Stretch" Grid.Column="0" Grid.Row="3" Text="KIN" Margin="4,0,0,0" VerticalAlignment="Center"/>
                                <TextBlock HorizontalAlignment="Stretch" Grid.Column="0" Grid.Row="4" Text="SON" Margin="4,0,0,0" VerticalAlignment="Center"/>

                                <TextBox HorizontalAlignment="Stretch" Grid.Row="0" Grid.Column="1" Text="{Binding Jug}" Margin="4,0,0,0" VerticalAlignment="Center">
                                </TextBox>

                                <TextBox HorizontalAlignment="Stretch" Grid.Row="1" Grid.Column="1" Text="{Binding Sch}" Margin="4,0,0,0" VerticalAlignment="Center">
                                </TextBox>

                                <TextBox HorizontalAlignment="Stretch" Grid.Row="2" Grid.Column="1" Text="{Binding Elt}" Margin="4,0,0,0" VerticalAlignment="Center">
                                </TextBox>

                                <TextBox HorizontalAlignment="Stretch" Grid.Row="3" Grid.Column="1" Text="{Binding Kin}" Margin="4,0,0,0" VerticalAlignment="Center">
                                </TextBox>

                                <TextBox HorizontalAlignment="Stretch"   Grid.Row="4" Grid.Column="1" Text="{Binding Son}" Margin="4,0,0,0" VerticalAlignment="Center">
                                </TextBox>
                            </Grid>
                        </Grid>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
        </Grid>
    </Grid>
</UserControl>
    • ViewModel:**
internal class InformationsViewModel : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    public ObservableCollection<InformationsItem> informations = new ObservableCollection<InformationsItem>();

    public InformationsViewModel()
    {

        for (int i = 0; i < 9; i++)
        {
            Informations.Add(new InformationsItem("Name", "Information", "Jug", "Sch", "Elt", "Kin", "Son"));
        }

    }


    public ObservableCollection<InformationsItem> Informations
    {
        get { return informations; }
        set
        {
            informations = value;
            NotifyPropertyChanged("Informations");
        }
    }

    public void NotifyPropertyChanged([CallerMemberName] string propertyName = "")
    {
        if (PropertyChanged != null)
        {
            this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }

    }
}
    • 型号:**
internal class InformationsItem : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    string name;
    string information;
    string jug;
    string sch;
    string elt;
    string kin;
    string son;

    public InformationsItem(string name, string information = "", string jug = "", string sch = "", string elt = "", string kin = "", string son = "")
    {
        this.name = name;
        this.information = information;
        this.jug = jug;
        this.sch = sch;
        this.elt = elt;
        this.kin = kin;
        this.son = son;

    }

    public string Name
    {
        get { return name; }
        set
        {
            name = value;
            if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("Name"));
        }
    }

    public string Information
    {
        get { return information; }
        set
        {
            information = value;
            if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("Information"));
        }
    }

    public string Jug
    {
        get { return jug; }
        set
        {
            jug = value;
            if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("Jug"));
        }
    }

    public string Sch
    {
        get { return sch; }
        set
        {
            sch = value;
            if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("Sch"));
        }
    }
    public string Elt
    {
        get { return elt; }
        set
        {
            elt = value;
            if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("Elt"));
        }
    }

    public string Kin
    {
        get { return kin; }
        set
        {
            kin = value;
            if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("Kin"));
        }
    }

    public string Son
    {
        get { return son; }
        set
        {
            son = value;
            if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("Son"));
        }
    }

}

正如您在这个例子中看到的,RichTextBox并没有使用变量列的整个宽度。RichTextBox保持较小,但应与列一样大。这里有什么问题?我应该把RichTextBox的宽度绑定到什么东西上吗?
感谢您的评分

相关问题