WPF:如何使用自定义用户控件在 ListView 中设置自动填充的列宽

发布于 2024-09-02 21:00:44 字数 2170 浏览 6 评论 0原文

GridViewColumn 中带有 Datatemplate 的 ListView:

    <ListView Name ="LogDataList" IsSynchronizedWithCurrentItem="True" ItemsSource="{Binding LogDataCollection}" Background="Cyan"> 
            <ListView.View> 
                <GridView AllowsColumnReorder="true" 
                  ColumnHeaderToolTip="Event Log Information"> 
                    <GridViewColumn Header="Event Log Name" Width="100"> 
                        <GridViewColumn.CellTemplate> 
                            <DataTemplate> 
                                <l:MyTextBlock Height="25" DataContext="{Binding LogName, Converter={StaticResource DataFieldConverter}}" HighlightMatchCase="{Binding Element}" Loaded="EditBox_Loaded"/> 
                            </DataTemplate> 
                        </GridViewColumn.CellTemplate> 
                    </GridViewColumn> 
                        ...
                    </GridView> 
            </ListView.View> 
        </ListView> 

尽管我尝试了很多方法,但我不知道如何使列宽自动填充。 演示的总体思路是:

<ListView Name ="LogDataList" IsSynchronizedWithCurrentItem="True" ItemsSource="{Binding LogDataCollection}" Background="Cyan">
            <ListView.Resources>
                <Style x:Key="ColumnWidthStyle" TargetType="{x:Type GridViewColumn}">
                    <Style.Setters>
                        <Setter Property="HorizontalContentAlignment" Value="Stretch" >
                        </Setter>
                    </Style.Setters>
                </Style>
            </ListView.Resources>
            <ListView.View>
                <GridView AllowsColumnReorder="true"
                  ColumnHeaderToolTip="Event Log Information">
                    <GridViewColumn Header="Event Log Name" DisplayMemberBinding="{Binding Path=LogName}" HeaderContainerStyle="{StaticResource ColumnWidthStyle}">

它可以工作,但不符合我的需求。由于增强功能(HighlighMatchCase 属性)和绑定数据上下文,我需要使用自定义用户控件(MyTextBlock)自定义数据模板。

如何设置 ColumnWidthMode 并填写单词?上线了。

我真的很感谢你的帮助。

A ListView with Datatemplate in GridViewColumn:

    <ListView Name ="LogDataList" IsSynchronizedWithCurrentItem="True" ItemsSource="{Binding LogDataCollection}" Background="Cyan"> 
            <ListView.View> 
                <GridView AllowsColumnReorder="true" 
                  ColumnHeaderToolTip="Event Log Information"> 
                    <GridViewColumn Header="Event Log Name" Width="100"> 
                        <GridViewColumn.CellTemplate> 
                            <DataTemplate> 
                                <l:MyTextBlock Height="25" DataContext="{Binding LogName, Converter={StaticResource DataFieldConverter}}" HighlightMatchCase="{Binding Element}" Loaded="EditBox_Loaded"/> 
                            </DataTemplate> 
                        </GridViewColumn.CellTemplate> 
                    </GridViewColumn> 
                        ...
                    </GridView> 
            </ListView.View> 
        </ListView> 

I have no idea about how to make column width autofill although I have tried a lot of way to walk up.
The general idea for demo is :

<ListView Name ="LogDataList" IsSynchronizedWithCurrentItem="True" ItemsSource="{Binding LogDataCollection}" Background="Cyan">
            <ListView.Resources>
                <Style x:Key="ColumnWidthStyle" TargetType="{x:Type GridViewColumn}">
                    <Style.Setters>
                        <Setter Property="HorizontalContentAlignment" Value="Stretch" >
                        </Setter>
                    </Style.Setters>
                </Style>
            </ListView.Resources>
            <ListView.View>
                <GridView AllowsColumnReorder="true"
                  ColumnHeaderToolTip="Event Log Information">
                    <GridViewColumn Header="Event Log Name" DisplayMemberBinding="{Binding Path=LogName}" HeaderContainerStyle="{StaticResource ColumnWidthStyle}">

It works, but not accord with my demand. I need to customize datatemplate with my custom user control(MyTextBlock) since the enhancement(HighlighMatchCase property) and binding datacontext.

How can I set up ColumnWidthMode with Fill in the word? On-line'in.

I really appreciate your help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

沙沙粒小 2024-09-09 21:00:44

这对我来说是工作。首先,将 Text 属性添加到 MyTextBlock,因为它不是从 System.Windows.Controls.TextBlock 继承的,而是从 User Control 继承的。

public object Text
        {
            get { return GetValue(TextProperty); }
            set
            {
                SetValue(TextProperty, value);
            }
        }

        public static readonly DependencyProperty TextProperty =
            DependencyProperty.Register(
        "Text",
        typeof(object),
        typeof(MyTextBlock),
                    new PropertyMetadata(null, new PropertyChangedCallback(TextChangedCallback)));

        static void TextChangedCallback(DependencyObject property,
DependencyPropertyChangedEventArgs args)
        {
            MyTextBlock textBox = (MyTextBlock)property;
            textBox.textBlock.Text = args.NewValue.ToString();
        }

然后,手动调整列宽,如下所示:

private void ResizeColumnWidth()
        {
            foreach (GridViewColumn column in LogGridView.Columns)
            {
                column.Width = column.ActualWidth;
                column.Width = double.NaN;
            }
        }

This is work for me. First, add Text property to MyTextBlock since it is not inherited from System.Windows.Controls.TextBlock but User Control.

public object Text
        {
            get { return GetValue(TextProperty); }
            set
            {
                SetValue(TextProperty, value);
            }
        }

        public static readonly DependencyProperty TextProperty =
            DependencyProperty.Register(
        "Text",
        typeof(object),
        typeof(MyTextBlock),
                    new PropertyMetadata(null, new PropertyChangedCallback(TextChangedCallback)));

        static void TextChangedCallback(DependencyObject property,
DependencyPropertyChangedEventArgs args)
        {
            MyTextBlock textBox = (MyTextBlock)property;
            textBox.textBlock.Text = args.NewValue.ToString();
        }

Then, resize column width manually like this:

private void ResizeColumnWidth()
        {
            foreach (GridViewColumn column in LogGridView.Columns)
            {
                column.Width = column.ActualWidth;
                column.Width = double.NaN;
            }
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文