XAML CollectionViewSource中的SelectedItem

时间:2014-12-06 15:23:34

标签: c# xaml

我试图在我的Xaml中使用SelectedItem并显示其属性的som。以下是观点:

<UserControl.Resources>      
        <CollectionViewSource   x:Name="cvs"
                                IsSourceGrouped="True"
                                Source="{Binding Path=GroupedPeople}"
                                ItemsPath="Items"/>
    </UserControl.Resources>

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="2*" />
            <RowDefinition Height="1*" />
        </Grid.RowDefinitions>
        <ScrollViewer 
                    HorizontalScrollBarVisibility="Auto" 
                    VerticalScrollBarVisibility="Auto">                    
            <Grid Grid.Row="0" Background="DarkSeaGreen">
                <GridView x:Name="myGridView"> 
                    <ItemsControl.ItemsPanel>
                        <ItemsPanelTemplate>
                            <StackPanel Orientation="Horizontal" />
                        </ItemsPanelTemplate>
                    </ItemsControl.ItemsPanel>

                    <ItemsControl.ItemContainerStyle>
                        <Style TargetType="GridViewItem">
                            <Setter Property="HorizontalContentAlignment"
                            Value="Left" />
                            <Setter Property="Padding"
                            Value="5" />
                        </Style>
                    </ItemsControl.ItemContainerStyle>

                    <ItemsControl.GroupStyle>
                        <GroupStyle>
                            <GroupStyle.HeaderTemplate>
                                <DataTemplate>
                                    <TextBlock Text="{Binding Key}"
                                        FontSize="26.67" />
                                </DataTemplate>
                            </GroupStyle.HeaderTemplate>

                            <GroupStyle.Panel>
                                <ItemsPanelTemplate>
                                    <VariableSizedWrapGrid Orientation="Vertical" ItemWidth="220" />
                                </ItemsPanelTemplate>
                            </GroupStyle.Panel>

                        </GroupStyle>
                    </ItemsControl.GroupStyle>
                </GridView>

            </Grid>
        </ScrollViewer>

        <Grid HorizontalAlignment="Left" Height="177" Margin="708,37,0,0" Grid.Row="1" VerticalAlignment="Top" Width="558">

            <TextBlock Text="{Binding SelectedPerson.Name}"></TextBlock>

        </Grid>

正如您在底部网格中看到的那样,我想显示SelectedItem。 在CSV上使用SelectedItem似乎存在问题,例如我无法做到:

<CollectionViewSource   x:Name="cvs"
                                IsSourceGrouped="True"
                                Source="{Binding Path=GroupedPeople}"
                                ItemsPath="Items"
                                SelectedItem="{Binding SelectedPerson}"/>

Beacuse SelectedItem在这里不起作用。 我的代码中有这个方法:

private Person _selectedPerson;

public Person SelectedPerson
{
    get { return _selectedPerson; }
    set
    {
        _selectedPerson = value;
        RaisePropertyChanged("SelectedPerson");
    }
}

但似乎永远不会受到打击。 帮助赞赏。谢谢!

0 个答案:

没有答案