【问题标题】:Listview item text truncated after itemssource change在 itemssource 更改后,Listview 项目文本被截断
【发布时间】:2012-03-15 09:38:54
【问题描述】:

我有 WPF ListView 和 MVVM。 ListView 是简单的父子结构的一部分。 Parent也是ListView控件。当我更改父控件中的选定项时,子控件的 ItemsSource 会更新。例如,如果在第一个 ItemsSource 中项目的最长文本包含 5 个字符,则在更改 ItemsSource 后,每个项目的文本最多可见 5 个字符(在新的 ItemsSource 中)。如何解决这个问题?

代码示例:

    <ListView Grid.Row="0" Grid.Column="3" ItemsSource="{Binding Tasks}" Width="200" Height="250" VerticalAlignment="Bottom" SelectionMode="Extended">
        <ListView.View>
            <GridView>
                <GridView.ColumnHeaderContainerStyle>
                    <Style TargetType="{x:Type GridViewColumnHeader}">
                        <Setter Property="Visibility" Value="Collapsed"/>
                    </Style>
                </GridView.ColumnHeaderContainerStyle>
                <GridViewColumn Header="Name" DisplayMemberBinding="{Binding Name}"/>
            </GridView>
        </ListView.View>
        <ListView.ItemContainerStyle>
            <Style TargetType="{x:Type ListViewItem}" BasedOn="{StaticResource {x:Type ListViewItem}}">
                <Setter Property="IsSelected" Value="{Binding Mode=TwoWay, Path=IsSelected}"/>
            </Style>
        </ListView.ItemContainerStyle>
    </ListView>

我使用的是 BureauBlue 主题,但无论如何,如果我不使用它,我也会遇到同样的问题。

ViewModel中的部分代码:

    private void MyViewModel_PropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        if (e.PropertyName == "CurrentParentItem")
        {
            if (this.CurrentParentItem != null)
            {
                this.Tasks = GetTasks();//ObservableCollection
            }
            else
            {
                this.Tasks = null;
            }
        }
    }

【问题讨论】:

  • 您的 ListView 是否足够宽以容纳较长的文本?
  • 是的。例如,当它被切断时,它只占用列表视图宽度的 10%。
  • 你能给我们提供 ListView 的 xaml 吗?

标签: wpf listviewitem truncated


【解决方案1】:

GridView 不会使用新数据重新计算宽度。要重置宽度,您可以使用以下

            foreach (GridViewColumn c in gv.Columns)
            {
                // Code below was found in GridViewColumnHeader.OnGripperDoubleClicked() event handler (using Reflector)
                // i.e. it is the same code that is executed when the gripper is double clicked
                // if (adjustAllColumns || App.StaticGabeLib.FieldDefsGrid[colNum].DispGrid)
                if (adjustAllColumns || fdGridSorted[colNum].AppliedDispGrid)
                {
                    if (double.IsNaN(c.Width))
                    {
                        c.Width = c.ActualWidth;
                    }
                    c.Width = double.NaN;
                }
            }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-12-04
    • 1970-01-01
    • 2012-07-01
    • 2018-03-23
    • 2014-01-26
    • 1970-01-01
    • 2017-06-30
    相关资源
    最近更新 更多