WPF: Как установить ширину столбца с автоматическим заполнением в ListView с пользовательским управлением - PullRequest
0 голосов
/ 27 мая 2010

ListView с табличкой данных в 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> 

Я понятия не имею, как сделать автозаполнение ширины столбца, хотя я много пробовал подойти. Общая идея для демонстрации:

<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}">

Это работает, но не соответствует моему требованию. Мне нужно настроить табличку данных с моим пользовательским элементом управления (MyTextBlock), начиная с расширения (свойство HighlighMatchCase) и привязки datacontext.

Как настроить ColumnWidthMode с помощью Fill in the word? On-line'in.

Я очень ценю вашу помощь.

1 Ответ

0 голосов
/ 30 мая 2010

Это работа для меня. Сначала добавьте свойство Text в MyTextBlock, поскольку оно не наследуется от System.Windows.Controls.TextBlock, но контролируется пользователем.

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;
            }
        }
...