0

我的 GridView 项目有以下数据模板。我想更改 GridView 中选定项的数据模板。

girdView 的数据模板

<DataTemplate x:Key="SearchListItemDataTemplate1">
    <Grid Height="auto" Background="Red">
        <Grid Height="auto" Margin="14,0">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="110"></ColumnDefinition>
                <ColumnDefinition Width="*"></ColumnDefinition>
                <ColumnDefinition Width="55"></ColumnDefinition>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="*"></RowDefinition>
                <RowDefinition Height="auto"></RowDefinition>
            </Grid.RowDefinitions>

            <!--Image Section-->
            <Grid Height="108" Margin="0,8,0,0" VerticalAlignment="Top">
                <Grid Margin="0">
                    <Border HorizontalAlignment="Left" Margin="0" VerticalAlignment="Bottom" Width="98" Height="98" CacheMode="BitmapCache" CornerRadius="0,0,30,0">

                    </Border>
                    <Border Background="#FFf26f46"
                                            Width="40" Height="40"
                                            VerticalAlignment="Bottom" 
                                            HorizontalAlignment="Left"
                                            CornerRadius="0,0,12,0">
                        <Grid>
                            <StackPanel  HorizontalAlignment="Center" VerticalAlignment="Center" Orientation="Horizontal">
                                <TextBlock Text="{Binding Degree}" FontSize="20" Foreground="White" VerticalAlignment="Center" />
                                <TextBlock VerticalAlignment="Center" Margin="0,3,0,0" FontSize="16" Foreground="White" Text="{Binding ConnectionDegree,Converter={StaticResource degreeToTextConverter}}" />
                            </StackPanel>
                        </Grid>
                    </Border>
                </Grid>
                <!--<Ellipse VerticalAlignment="Top" Margin="0,0,0,0" HorizontalAlignment="Right" StrokeThickness="0" Fill="{Binding Status,Converter={StaticResource statusToColorBrushConverter}}" Width="20" Height="20"></Ellipse>-->
            </Grid>
  </Grid>

现在,用户在此 GridView 中选择了任何项目我想更改所选项目的 DataTemplate,如下所示。

<DataTemplate x:Key="SearchListItemDataTemplate1">
    <Grid Height="auto" Background="Red">
        <Grid Height="auto" Margin="14,0">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="110"></ColumnDefinition>
                <ColumnDefinition Width="*"></ColumnDefinition>
                <ColumnDefinition Width="55"></ColumnDefinition>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="*"></RowDefinition>
                <RowDefinition Height="auto"></RowDefinition>
            </Grid.RowDefinitions>

            <!--Image Section-->
            <Grid Height="108" Margin="0,8,0,0" VerticalAlignment="Top">
                <Grid Margin="0">
                    <Border HorizontalAlignment="Left" Margin="0" VerticalAlignment="Bottom" Width="98" Height="98" CacheMode="BitmapCache" CornerRadius="0,0,30,0">

                    </Border>
                    <Border Background="#FFf26f46"
                                            Width="40" Height="40"
                                            VerticalAlignment="Bottom" 
                                            HorizontalAlignment="Left"
                                            CornerRadius="0,0,12,0">
                        <Grid>
                            <StackPanel  HorizontalAlignment="Center" VerticalAlignment="Center" Orientation="Horizontal">
                                <TextBlock Text="{Binding Degree}" FontSize="20" Foreground="White" VerticalAlignment="Center" />
                                <TextBlock VerticalAlignment="Center" Margin="0,3,0,0" FontSize="16" Foreground="White" Text="{Binding ConnectionDegree,Converter={StaticResource degreeToTextConverter}}" />
                            </StackPanel>
                        </Grid>
                    </Border>
                </Grid>
                <!--<Ellipse VerticalAlignment="Top" Margin="0,0,0,0" HorizontalAlignment="Right" StrokeThickness="0" Fill="{Binding Status,Converter={StaticResource statusToColorBrushConverter}}" Width="20" Height="20"></Ellipse>-->
            </Grid>

            <!-- Details Section  -->
            <Grid Grid.Column="1" Margin="10,8">
                <Grid.RowDefinitions>
                    <RowDefinition Height="auto"></RowDefinition>
                    <RowDefinition Height="auto"></RowDefinition>
                    <RowDefinition Height="auto"></RowDefinition>
                    <RowDefinition Height="*"></RowDefinition>
                    <RowDefinition Height="auto"></RowDefinition>
                </Grid.RowDefinitions>

                <TextBlock Foreground="#FF00579b" FontSize="20" TextWrapping="NoWrap" TextTrimming="CharacterEllipsis" Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="3"
               localpage:SearchView.InlineList="{Binding RunElements}" />

                <TextBlock Text="{Binding Name}" Grid.Row="1" Foreground="#FF575757" FontSize="16" Margin="0,4,0,0"></TextBlock>
                <TextBlock Text="{Binding Details}" Grid.Row="2" Height="auto" Foreground="#FF575757" FontSize="16" Margin="0,4,0,0"></TextBlock>

                <TextBlock Text="{Binding TheTextB}" VerticalAlignment="Bottom" Grid.Row="4" Foreground="#FF8fa4ad" FontSize="14" Margin="0,10,0,0"></TextBlock>
            </Grid>

            <!-- Operation Section Visibility="{Binding IsSelected, Converter={StaticResource boolToVisibiliytConverter},ConverterParameter=-1}"-->
            <Grid Grid.Column="2">
                <Button Content="{Binding ActionText}" Command="{Binding TheAction}" CommandParameter="{Binding Id}" Width="55" MinWidth="55" FontSize="16" Foreground="#FF42a046" Style="{Binding ActionStyle}"
                        />
            </Grid>


        </Grid>
    </Grid>
</DataTemplate>

我已阅读有关模板选择器的信息。但它会在绑定时选择模板。

有没有办法更改所选项目的数据模板?

4

1 回答 1

1

如果我正确阅读了您的模板,您希望在用户选择一个项目后显示一些额外的详细信息,对吗?更改模板并不是最好的方法。

您应该在您的 itemViewModel 上添加某种“ShowDetails”属性(或您的 gridview 中的任何项目类型),然后根据该属性更改模板内的可视化。

您仍然可以使用 gridview 的 SelectionChanged 事件在所选项目上设置该属性,或者只需在模板中放置一个触发更改的按钮。

于 2014-12-11T12:59:36.383 回答