在Expander WPF上调整大小的问题

时间:2012-11-01 18:28:00

标签: wpf mvvm resize expander

大家好我正在使用WPF和mvvm解决方案,我有一个问题。 我有这个对象,我在viewModel上使用:

public class SuperCharacter : INotifyPropertyChanged
{
    public List<Character> Characters { get; set; }
    public string Name { get; set; }
    private Character charactersExp;
    private const string currentCharacterExpandedString = "CurrentCharacterExp";
    public Character CurrentCharacterExpanded
    {
        get { return this.charactersExp; }
        set
        {
            this.charactersExp = value;
            this.OnPropertyChanged(currentCharacterExpandedString);
        }
    }

    public string CalcSize { get; set; }

    public void OnPropertyChanged(string propertyName)
    {


        if (this.PropertyChanged != null)
        {
            this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }

    }

    public event PropertyChangedEventHandler PropertyChanged;
}

我有这个观点:

    Window.Resources>


    <DataTemplate x:Key="TrackPointUserSavedSearchDtoTemplate" DataType="{x:Type src:Character}">
        <StackPanel >
            <TextBlock x:Name="caption" Margin="1" 
        Text="{Binding First}" MaxWidth="{Binding ElementName=image, Path=ActualWidth}" MaxHeight="40" />
        </StackPanel>
    </DataTemplate>

    <DataTemplate x:Key="DynamicUserSaveSearchesTemplate" DataType="{x:Type src:Character}">
        <ContentPresenter x:Name="content" ContentTemplate="{StaticResource TrackPointUserSavedSearchDtoTemplate    }"/>
    </DataTemplate>

    <DataTemplate x:Key="IconoTrackPointUSTemplate" DataType="{x:Type src:SuperCharacter}">
        <ScrollViewer Grid.Row="2" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
            <Expander Height="180" Margin="12,0,0,-127" >

                <Expander.Header>
                    <Binding Path="Name"></Binding>
                </Expander.Header>                   

                    <ListView Name="ProblemListView" HorizontalContentAlignment="Stretch"  ItemsSource="{Binding Characters}" SelectedItem="{Binding CurrentCharacterExpanded}"  ItemTemplate="{StaticResource DynamicUserSaveSearchesTemplate}" Panel.ZIndex="20">
                    </ListView>                       
            </Expander>
        </ScrollViewer>
    </DataTemplate>

    <DataTemplate x:Key="DynamicTrackPointUSTemplate" DataType="{x:Type src:SuperCharacter}" >
        <ContentPresenter x:Name="content" ContentTemplate="{StaticResource IconoTrackPointUSTemplate    }"/>
    </DataTemplate>
</Window.Resources>
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="42"></RowDefinition>
        <RowDefinition Height="Auto"></RowDefinition>           
    </Grid.RowDefinitions>
    <Label Content="Entitty's TrackPoint list:" Margin="12,5,76,9" Name="labelName" />
    <ListView Grid.Row="1" ItemsSource="{Binding SuperCharacters}" SelectedItem="{Binding CurrentSuperCharacterExpanded}" ItemTemplate="{StaticResource DynamicTrackPointUSTemplate}">
    </ListView>
<Grid/>

问题是当我展开Expander时,Listview调用的问题problemmListView位于下一个扩展器的下方。

我想知道如何让这个扩展程序列表看起来正确?如果我展开一个扩展器,problemListView会正确显示。

请记住,列表是动态的,可以包含不同数量的元素

1 个答案:

答案 0 :(得分:0)

首先,我认为在Grid.Row="2"中为ScrollViewer设置DataTemplate没有多大意义。其次,margin的{​​{1}}造成了麻烦,我不明白为什么要设置这个底边(-127)。

您需要为Expander指定Height,否则网格行只会随着ScrollViewer的增长而扩展。

尝试对'IconoTrackPointUSTemplate:

进行这些更改
ListView

就像提示一样,请注意我如何更改扩展程序头的绑定,您可以在内联中节省相当多的空间:

<DataTemplate x:Key="IconoTrackPointUSTemplate" DataType="{x:Type WpfApplication1:SuperCharacter}">
    <ScrollViewer Height="50" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
        <Expander Margin="12,0,0,0" Header="{Binding Name}">
            <ListView Name="ProblemListView" 
                        HorizontalContentAlignment="Stretch" 
                        ItemsSource="{Binding Characters}" 
                        SelectedItem="{Binding CurrentCharacterExpanded}" 
                        ItemTemplate="{StaticResource DynamicUserSaveSearchesTemplate}" 
                        Panel.ZIndex="20" />
        </Expander>
    </ScrollViewer>
</DataTemplate>
相关问题