具有水平布局的 ListView 并从后面的代码换行
ListView with horizontal layout and wrap from code behind
我被迫使用来自代码隐藏的 ListView/listbox。
在那我必须设置边界。我希望他们在需要时换行
ListView lsv = new ListView
{
Width = 400,
Height = 200,
Background = Brushes.LimeGreen,
};
grdMain.Children.Add(lsv);
for (int iii = 0; iii < 15; iii++)
{
Border b = new Border() { BorderThickness = new Thickness(3),
BorderBrush = new SolidColorBrush(Colors.Blue), Width = 50, Height = 50
};
lsv.Items.Add(b);
lsv.ItemsPanel = (ItemsPanelTemplate)XamlReader.Parse("<ItemsPanelTemplate xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\"><WrapPanel Orientation=\"Horizontal\" HorizontalAlignment=\"Stretch\"/></ItemsPanelTemplate>");
现在效果如下:
相反,如果我放置固定宽度 Width=\"300\"
lsv.ItemsPanel = (ItemsPanelTemplate)XamlReader.Parse("<ItemsPanelTemplate xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\"><WrapPanel Orientation=\"Horizontal\" Width=\"300\"/></ItemsPanelTemplate>");
它工作正常
问题是我无法为环绕面板设置固定宽度,我希望它扩展以填充其父项。
提前致谢
帕特里克
您可以尝试执行以下操作...
ListView listView = new ListView();
var scrollViewer = Utils.GetDescendantByType(listView, typeof(ScrollViewer)) as ScrollViewer;
scrollViewer.HorizontalScrollBarVisibility = ScrollBarVisibility.Disabled;
哪里会有辅助方法
public static class Utils
{
public static Visual GetDescendantByType(Visual element, Type type)
{
if (element == null)
return null;
if (element.GetType() == type)
return element;
Visual foundElement = null;
if (element is FrameworkElement)
(element as FrameworkElement).ApplyTemplate();
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(element); i++)
{
Visual visual = VisualTreeHelper.GetChild(element, i) as Visual;
foundElement = GetDescendantByType(visual, type);
if (foundElement != null)
break;
}
return foundElement;
}
}
注意,我已经在 phone.
上编译了这段代码
我被迫使用来自代码隐藏的 ListView/listbox。 在那我必须设置边界。我希望他们在需要时换行
ListView lsv = new ListView
{
Width = 400,
Height = 200,
Background = Brushes.LimeGreen,
};
grdMain.Children.Add(lsv);
for (int iii = 0; iii < 15; iii++)
{
Border b = new Border() { BorderThickness = new Thickness(3),
BorderBrush = new SolidColorBrush(Colors.Blue), Width = 50, Height = 50
};
lsv.Items.Add(b);
lsv.ItemsPanel = (ItemsPanelTemplate)XamlReader.Parse("<ItemsPanelTemplate xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\"><WrapPanel Orientation=\"Horizontal\" HorizontalAlignment=\"Stretch\"/></ItemsPanelTemplate>");
现在效果如下:
相反,如果我放置固定宽度 Width=\"300\"
lsv.ItemsPanel = (ItemsPanelTemplate)XamlReader.Parse("<ItemsPanelTemplate xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\"><WrapPanel Orientation=\"Horizontal\" Width=\"300\"/></ItemsPanelTemplate>");
它工作正常
问题是我无法为环绕面板设置固定宽度,我希望它扩展以填充其父项。
提前致谢 帕特里克
您可以尝试执行以下操作...
ListView listView = new ListView();
var scrollViewer = Utils.GetDescendantByType(listView, typeof(ScrollViewer)) as ScrollViewer;
scrollViewer.HorizontalScrollBarVisibility = ScrollBarVisibility.Disabled;
哪里会有辅助方法
public static class Utils
{
public static Visual GetDescendantByType(Visual element, Type type)
{
if (element == null)
return null;
if (element.GetType() == type)
return element;
Visual foundElement = null;
if (element is FrameworkElement)
(element as FrameworkElement).ApplyTemplate();
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(element); i++)
{
Visual visual = VisualTreeHelper.GetChild(element, i) as Visual;
foundElement = GetDescendantByType(visual, type);
if (foundElement != null)
break;
}
return foundElement;
}
}
注意,我已经在 phone.
上编译了这段代码