WPF - 调整列/行对齐的大小

时间:2016-01-20 13:48:06

标签: c# wpf listview resize alignment

我想在WPF(2)中创建一个List-view,它在重新调整大小时不会改变列和行之间的间距,但会自动更改列数以适应。

我有什么:

我想要什么

代码:

<Window x:Class="WPFApplication.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"       
    Title="Test1" Height="800" Width="1000" Loaded="Window_Loaded" Closing="Window_Closing">
<Window.Resources>
    <DataTemplate x:Key="ItemTemplate">
        <WrapPanel>
            <Grid>
                <Border BorderThickness="3" BorderBrush="#FF000000" VerticalAlignment="Top">
                    <Image Width="200" Height="300" Stretch="Fill" DataContext="{DynamicResource Image}" Source="{Binding Image}"/>
                </Border>
                <Image HorizontalAlignment="Right" VerticalAlignment="Top" DataContext="{DynamicResource Data}" Source="{Binding Status}" Height="100"/>                    
            </Grid>
        </WrapPanel>
    </DataTemplate>
</Window.Resources>
<Grid x:Name="mainGrid" Margin="0,81,0,0">
    <ListView HorizontalContentAlignment="Left" VerticalContentAlignment="Top" Name="View" ItemTemplate="{StaticResource ItemTemplate}" ItemsSource="{Binding Path = DataList}">
        <ListView.ItemContainerStyle>
            <Style TargetType="ListViewItem">
                <Setter Property="Margin" Value="5"/>
                <Setter Property="HorizontalAlignment" Value="Left"/>
                <Setter Property="VerticalAlignment" Value="Top"/>
            </Style>
        </ListView.ItemContainerStyle>
        <ListView.ItemsPanel>
            <ItemsPanelTemplate>
                <UniformGrid Columns="3"/>
            </ItemsPanelTemplate>
        </ListView.ItemsPanel>
    </ListView>
</Grid>

1 个答案:

答案 0 :(得分:0)

我找到了一个解决方案,我改变了这个:

import numpy as np
import time


# Generate some random data.
N = 100
x, y, z = [np.random.uniform(0., 10., N) for _ in range(3)]

# Grid of values in 2 dimensions.
M = 200
p_lst, q_lst = np.linspace(0., 50., M), np.linspace(0., 25., M)

# Define empty nested list to be filled below.
# The shape is given by the length of the lists defined above.
abc_lst = [[[] for _ in p_lst] for _ in q_lst]

# First block. This needs to be separated from the block below.
# Fill nested list with values.
for i, p in enumerate(p_lst):
    for j, q in enumerate(q_lst):

        # a,b,c are obtained via some complicated function of p,q.
        # This is just for the purpose of this example.
        a, b, c = 1.*p, 1.*q, p+q

        # Store in nested list.
        abc_lst[i][j] = [a, b, c]

# Second block <-- THIS IS THE BOTTLENECK
tik = time.time()
# Apply operation on nested list.
lst = []
for i in range(len(p_lst)):
    for j in range(len(q_lst)):

        # Extract a,b,c values from nested list.
        a, b, c = abc_lst[i][j]

        # Apply operation. This is the *actual* operation
        # I need to apply.
        d = sum(abs(a*x + y*b + c*z))

        # Store value.
        lst.append(d)

print time.time() - tik

到此:

 <ItemsPanelTemplate>
            <UniformGrid Columns="3"/>
 </ItemsPanelTemplate>