【问题标题】:UWP RichEditBox initially shows just one line in ListViewUWP RichEditBox 最初在 ListView 中只显示一行
【发布时间】:2020-02-29 12:24:22
【问题描述】:

我已经尝试过这里建议的解决方案https://social.msdn.microsoft.com/Forums/windowsapps/en-US/62d36441-7b14-4788-b146-2a85b9fc7a00/binding-richeditbox-in-xaml-from-codebehind:

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <ListView x:Name="MyListView">
        <ListView.ItemTemplate>
            <DataTemplate>
                <RichEditBox local:BindableREBText.Text="{Binding}" ></RichEditBox>                    
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
</Grid>

看起来它正在工作,但底层控件有一个错误,使其无用 - 控件仅显示一行文本,直到您将指针放在它获得适当大小时。我试图在分配文本后强制执行该措施,但到目前为止我无法使其工作。任何人都知道如何解决这个错误?

【问题讨论】:

  • 您能否分享一个截图来解释这一点,我们无法重现此问题。
  • @NicoZhu-MSFT 感谢您的评论。我已经从那个页面复制了代码,但是它设置了没有意义的宽度和高度,所以现在查看代码并检查它。我现在没有示例,但基本上它显示了列表项中小说的每一段,但 RichEditBox 折叠到文本的一行,直到您将鼠标放在上面。如果仍然无法重现,我将在 github 上创建一个示例。
  • 你的项目是否像this一样工作。
  • @NicoZhu-MSFT,不 - 正如上面的评论中所说,我在这里发布了示例中的代码,它包括不应该存在的宽度限制,看来你做到了同样根据屏幕截图,它可能会影响行为。

标签: xaml uwp


【解决方案1】:

UWP RichEditBox 最初在 ListView 中只显示一行

问题是您没有指定RichEditBox 的宽度,而RichEditBox 的宽度不适合ListViewitem。目前我们有一个解决方法,我们需要使用绑定方式来动态更改RichEditBox 的宽度。

public HomePage()
{
    this.InitializeComponent();
    this.Loaded += HomePage_Loaded;
    this.DataContext = this;
}
private double _rdbWidth;
public double RDBWidth
{
    get
    {
        return _rdbWidth;
    }

    set
    {
        _rdbWidth = value;
        OnPropertyChanged();
    }


}
private void MyListView_SizeChanged(object sender, SizeChangedEventArgs e)
{
    RDBWidth = MyListView.ActualWidth;
}

public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged([CallerMemberName] string proprtyName = null)
{
    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(proprtyName));
}

Xaml 代码

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <ListView x:Name="MyListView" SizeChanged="MyListView_SizeChanged">
        <ListView.ItemContainerStyle>
            <Style TargetType="ListViewItem">
                <Setter Property="Padding" Value="0,0,0,0" />
                <Setter Property="Margin" Value="0,0,0,0" />
            </Style>
        </ListView.ItemContainerStyle>

        <ListView.ItemTemplate>
            <DataTemplate>
                <RichEditBox
                    x:Name="MyRdb"
                    Width="{Binding ElementName=MyListView, Path=DataContext.RDBWidth}"
                    Margin="0"
                    local:BindableREBText.Text="{Binding}"
                    TextAlignment="Justify"
                    />
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
</Grid>

【讨论】:

  • 另外报告一个问题,x:Bind 不适用于 RichEditBox.FontWeight,因为它适用于其他属性并且普通绑定也适用,我猜这是一种错误。跨度>
猜你喜欢
  • 1970-01-01
  • 2021-02-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多