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

时间:2010-05-27 16:05:41

标签: wpf listviewitem

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属性)和绑定datacontext以来,我需要使用自定义用户控件(MyTextBlock)自定义datatemplate。

如何在填充单词时设置ColumnWidthMode?上line'in。

我非常感谢你的帮助。

1 个答案:

答案 0 :(得分:0)

这对我有用。首先,将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;
            }
        }