Я использую поле ListView, расширяющее пользовательское свойство ListViewcolumns.Stretch.Таким образом, я могу иметь 3 столбца, которые располагаются равномерно независимо от размера окна.Моя проблема заключается в том, что в конце списка элементы «обрезаются»
. Таким образом, мой список содержит 3 столбца: красный, зеленый и черный. Когда я вставляю элемент, он вставляется в соответствующий столбец, а другойв два столбца вставляется пустой текст.Он вставляет элементы в положение 0, поэтому он всегда отображается сверху и опускает все остальное вниз.Полоса прокрутки по умолчанию отключена.
- Цель состоит в том, чтобы 3 столбца занимали все пространство окна независимо от того, насколько велико или мало окно, и соответственно изменяли размер текста (максимальная высота около 100-150).И затем, когда строки заполняют всю коллекцию, нижняя строка не может «выходить» из поля зрения, она должна быть либо полностью видимой, либо ни одной из них.
В любом случае - коды.Буду признателен за любую помощь, я могу жестко задавать максимальную высоту окна просмотра, пока не найду идеальный размер, подходящий для моего целевого монитора, но в идеале я хочу, чтобы он соответствующим образом изменил размеры
ListView
<ListView Grid.Column="1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Background="Transparent" Margin="15,10,5,5"
FontFamily="Impact" FontWeight="Bold" BorderThickness="0" VerticalContentAlignment="Stretch"
ScrollViewer.VerticalScrollBarVisibility="{Binding VerticleScrollVisibility}" ScrollViewer.HorizontalScrollBarVisibility="Auto"
ItemsSource="{Binding PlayNumbers}"
ItemContainerStyle="{StaticResource ListViewStyle}" ext:ListViewColumns.Stretch="True">
<ListView.View>
<GridView>
<GridView.ColumnHeaderContainerStyle>
<Style>
<Setter Property="FrameworkElement.Visibility" Value="Collapsed" />
</Style>
</GridView.ColumnHeaderContainerStyle>
<GridView.Columns>
<GridViewColumn Header="Red" CellTemplate="{StaticResource RedItemTemplate}" />
<GridViewColumn Header="Green" CellTemplate="{StaticResource GreenItemTemplate}" />
<GridViewColumn Header="Black" CellTemplate="{StaticResource BlackItemTemplate}" />
</GridView.Columns>
</GridView>
</ListView.View>
</ListView>
Файл скина
<!-- Templates for Number ListBox -->
<DataTemplate x:Key="RedItemTemplate">
<Viewbox MaxHeight="140" VerticalAlignment="Stretch">
<TextBlock Text="{Binding Path=RedItem}" HorizontalAlignment="Center" Foreground="Red" />
</Viewbox>
</DataTemplate>
<DataTemplate x:Key="GreenItemTemplate">
<Viewbox MaxHeight="140" VerticalAlignment="Stretch">
<TextBlock Text="{Binding Path=GreenItem}" HorizontalAlignment="Center" Foreground="Green" />
</Viewbox>
</DataTemplate>
<DataTemplate x:Key="BlackItemTemplate">
<Viewbox MaxHeight="140" VerticalAlignment="Stretch">
<TextBlock Text="{Binding Path=BlackItem}" HorizontalAlignment="Center" Foreground="LightGray" />
</Viewbox>
</DataTemplate>
<Style x:Key="ListViewStyle" TargetType="{x:Type ListViewItem}">
<Setter Property="Background" Value="Transparent" />
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListViewItem}">
<Border x:Name="Bd" Background="{TemplateBinding Background}"
SnapsToDevicePixels="True" BorderThickness="0,0,0,1" BorderBrush="Transparent">
<GridViewRowPresenter VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsSelected" Value="false" />
<Trigger Property="IsMouseOver" Value="false" />
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Пользовательское свойство
/// <summary>
/// ListViewColumnStretch
/// </summary>
public class ListViewColumns : DependencyObject
{
#region Fields
/// <summary>
/// Holds a reference to our parent ListView control
/// </summary>
private ListView _parentListView = null;
#endregion
#region Dependency Property Infrastructure
/// <summary>
/// IsStretched Dependency property which can be attached to gridview columns.
/// </summary>
public static readonly DependencyProperty StretchProperty =
DependencyProperty.RegisterAttached("Stretch",
typeof(bool),
typeof(ListViewColumns),
new UIPropertyMetadata(true, null, OnCoerceStretch));
/// <summary>
/// Gets the stretch.
/// </summary>
/// <param name="obj">The obj.</param>
/// <returns></returns>
public static bool GetStretch(DependencyObject obj)
{
return (bool)obj.GetValue(StretchProperty);
}
/// <summary>
/// Sets the stretch.
/// </summary>
/// <param name="obj">The obj.</param>
/// <param name="value">if set to <c>true</c> [value].</param>
public static void SetStretch(DependencyObject obj, bool value)
{
obj.SetValue(StretchProperty, value);
}
/// <summary>
/// Called when [coerce stretch].
/// </summary>
/// <remarks>If this callback seems unfamilar then please read
/// the great blog post by Paul jackson found here.
/// http://compilewith.net/2007/08/wpf-dependency-properties.html</remarks>
/// <param name="source">The source.</param>
/// <param name="value">The value.</param>
/// <returns></returns>
public static object OnCoerceStretch(DependencyObject source, object value)
{
ListView lv = (source as ListView);
//Ensure we dont have an invalid dependency object of type ListView.
if (lv == null)
throw new ArgumentException("This property may only be used on ListViews");
//Setup our event handlers for this list view.
lv.Loaded += new RoutedEventHandler(lv_Loaded);
lv.SizeChanged += new SizeChangedEventHandler(lv_SizeChanged);
return value;
}
#endregion
#region Event Handlers
/// <summary>
/// Handles the SizeChanged event of the lv control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="System.Windows.SizeChangedEventArgs"/> instance containing the event data.</param>
private static void lv_SizeChanged(object sender, SizeChangedEventArgs e)
{
ListView lv = (sender as ListView);
if (lv.IsLoaded)
{
//Set our initial widths.
SetColumnWidths(lv);
}
}
/// <summary>
/// Handles the Loaded event of the lv control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
private static void lv_Loaded(object sender, RoutedEventArgs e)
{
ListView lv = (sender as ListView);
//Set our initial widths.
SetColumnWidths(lv);
}
#endregion
#region Private Members
/// <summary>
/// Sets the column widths.
/// </summary>
private static void SetColumnWidths(ListView listView)
{
//Pull the stretch columns fromt the tag property.
List<GridViewColumn> columns = (listView.Tag as List<GridViewColumn>);
double specifiedWidth = 0;
GridView gridView = listView.View as GridView;
if (gridView != null)
{
if (columns == null)
{
//Instance if its our first run.
columns = new List<GridViewColumn>();
// Get all columns with no width having been set.
foreach (GridViewColumn column in gridView.Columns)
{
if (!(column.Width >= 0))
columns.Add(column);
else specifiedWidth += column.ActualWidth;
}
}
else
{
// Get all columns with no width having been set.
foreach (GridViewColumn column in gridView.Columns)
if (!columns.Contains(column))
specifiedWidth += column.ActualWidth;
}
// Allocate remaining space equally.
foreach (GridViewColumn column in columns)
{
double newWidth = (listView.ActualWidth - specifiedWidth) / columns.Count;
if (newWidth >= 0) column.Width = newWidth - 10;
}
//Store the columns in the TAG property for later use.
listView.Tag = columns;
}
}
#endregion
}