WPF listview列的百分比。你调用的对象是空的

时间:2015-07-26 08:54:11

标签: wpf listview size

我试图在ListView中设置百分比宽度,然后我找到了代码片段,怎么做,但是我把它放在我的代码和调试中,我收到一个错误:enter image description here

我的一列xaml代码是:

   <ListView x:Name="sample" Margin="30,98.4,362,150"  ScrollViewer.VerticalScrollBarVisibility="Visible" 
               AlternationCount="2" ScrollViewer.CanContentScroll="False">
            <ListView.View >
                <GridView ><!-- 1st Column-->
                <GridViewColumn>  
                    <GridViewColumnHeader  Content="Product" Margin="10,0,0,0"/>
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <StackPanel Orientation="Vertical">
                                <Grid Height="42px" SizeChanged="Size">
                                        <TextBlock Text="{Binding odd}" VerticalAlignment="Center"  
                                                 TextAlignment="Left"/>
                                    </Grid>
                                    <TextBlock  Foreground="#B30C0C" Height="42px" Padding="10,5,0,0" Text="{Binding discounText}" 
                                                Visibility="{Binding IsDiscount, Converter={StaticResource VisibilityConverter}}" />
                                </StackPanel>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>

我不知道在private void Size

中拨打<Grid Height="42px" SizeChanged="Size">是正确的地方

1 个答案:

答案 0 :(得分:0)

也许,你可以试试这个:

public static T FindParent<T>(DependencyObject child) where T : DependencyObject
{
    //get parent item
    DependencyObject parentObject = VisualTreeHelper.GetParent(child);

    //we've reached the end of the tree
    if (parentObject == null) return null;

    //check if the parent matches the type we're looking for
    T parent = parentObject as T;
    if (parent != null)
        return parent;
    else
        return FindParent<T>(parentObject);
}

private void Size(object sender, SizeChangedEventArgs e)
{
    // you handles Size event on Grid element, so sender is Grid
    Grid grid = sender as Grid;

    // FindParent<T> method searches parent of T type of DependencyObject in Visual Tree of your window
    GridView gView = FindParent<GridView>(grid);

    ListView listView = FindParent<ListView>(grid);

    //do whatever you want with your GridView here
}