Windows phone 8.1 xaml Listview选择了项目样式

时间:2014-08-23 12:06:47

标签: xaml winrt-xaml windows-phone-8.1

亲爱的所有我是Windows手机和xaml的新手。 我在我的数据透视模板上有listview现在我只想要当我在列表视图中选择一个项目时,这里更改的背景颜色是我的xaml代码

                        <ListView x:Name="LVPrimary" Grid.Row="2" Grid.Column="0" 
                        ItemsSource="{Binding}"
                        IsItemClickEnabled="True"
                        ItemClick="LVPrimary_ItemClick"  

                        ContinuumNavigationTransitionInfo.ExitElementContainer="True">

                        <ListView.ItemTemplate>
                            <DataTemplate>
                                <Grid>
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="auto"/>
                                        <ColumnDefinition Width="*"/>
                                    </Grid.ColumnDefinitions>
                                    <Border Width="40" Height="40" CornerRadius="5,5,5,5">
                                        <Border.Background>
                                            <ImageBrush ImageSource="{Binding ImagePath}" />
                                        </Border.Background>
                                    </Border>
                                    <StackPanel Grid.Row="0" Grid.Column="1" Margin="0,0,0,0">
                                        <TextBlock 
                                Text="{Binding Code}"
                                TextWrapping="Wrap"
                                Pivot.SlideInAnimationGroup="1"
                                CommonNavigationTransitionInfo.IsStaggerElement="True"
                                Style="{ThemeResource ListViewItemTextBlockStyle}"
                                Margin="0,0,19,0"/>
                                        <TextBlock
                                            TextWrapping="NoWrap" TextTrimming="CharacterEllipsis"
                                Text="{Binding Name}"
                                Pivot.SlideInAnimationGroup="2" 
                                CommonNavigationTransitionInfo.IsStaggerElement="True" 
                                Style="{ThemeResource ListViewItemContentTextBlockStyle}"
                                Margin="0,0,5,0"/>
                                    </StackPanel>
                                </Grid>
                            </DataTemplate>
                        </ListView.ItemTemplate>
                    </ListView>

2 个答案:

答案 0 :(得分:1)

很抱歉迟到的回复。您将拥有List<ItemClass>ObservableCollection<ItemClass>

您可以做的是 - 定义SolidColorBrush属性并将此属性绑定到您的项目网格。我创建了这个例子: -

以下是我的项目类: - 确保您实施INotifyPropertyChanged

public class Item : INotifyPropertyChanged
{
    public string Something { get; set; }

    public SolidColorBrush ItemBackground
    {
        get { return _itemBackground; }
        set
        {
            _itemBackground = value;
            OnPropertyChanged("ItemBackground");
        }
    }

    private SolidColorBrush _itemBackground;


    public event PropertyChangedEventHandler PropertyChanged;

    [NotifyPropertyChangedInvocator]
    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
    }
}

我的样本&amp;简单的Xaml是: -

<ListView x:Name="LVPrimary" IsItemClickEnabled="True" ItemClick="ListViewBase_OnItemClick">
        <ListView.ItemTemplate>
            <DataTemplate>
                <Grid Background="{Binding ItemBackground}" Height="100" Width="200">
                    <TextBlock Text="{Binding Something}"></TextBlock>
                </Grid>
            </DataTemplate>
        </ListView.ItemTemplate>
 </ListView>

在Onlick Event上我只是更改了点击项目的颜色: -

private void ListViewBase_OnItemClick(object sender, ItemClickEventArgs e)
{
        ((Item)e.ClickedItem).ItemBackground = new SolidColorBrush(Color.FromArgb(255, 255, 255, 255));
}

希望它会对你有所帮助。

答案 1 :(得分:1)

以下解决方案适用于我..

 var griditem = ListViewName.ContainerFromItem(ListViewName.SelectedItem);                     
 var a = ((Windows.UI.Xaml.Controls.Panel)((Windows.UI.Xaml.Controls.ContentControl)listViewItem).ContentTemplateRoot
 a.Background=  new SolidColorBrush(Windows.UI.Colors.Red);


 var textitem = ListViewName.ContainerFromItem(ListViewName.SelectedItem);                     
 var a = ((Windows.UI.Xaml.Controls.Panel)((Windows.UI.Xaml.Controls.ContentControl)listViewItem).ContentTemplateRoot).Children;
 ((TextBlock)a.ElementAt(0)).Text= "Your Text";