我正在为Windows Phone 8.1开发一个C#项目,我无法相信我已经浪费了近一天的时间来寻找解决这个小问题的方法:
我有一个用XAML定义的页面,在那个页面上我有一个ListView。在某些时候,我希望其中一个列表视图项被选中,所以我调用myListView.SelectedIndex = what。现在我希望该项目在视觉上与其他项目区分开来,例如,使用不同的颜色绘制其文本。我怎么做?以下是代码的相关部分:
<Page.Resources>
<DataTemplate x:Key="myListItemTemplate">
<TextBlock
Text="{Binding displayName}"
Style="{ThemeResource ListViewItemTextBlockStyle}"
/>
</DataTemplate>
</Page.Resources>
<ListView
x:Name="myListView"
ItemsSource="{Binding}"
ItemTemplate="{StaticResource myListItemTemplate}"
>
</ListView>
单独使用XAML是否可行?或者可以在C#代码中完成,就在我设置myListView.SelectedIndex值?
时谢谢!
答案 0 :(得分:26)
K,Andrei我认为所提供的解决方案非常好,它只是马车。这是我的。
XAML:注意SelectedUnfocused
<ListView x:Name="mylistview">
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListViewItem">
<Grid>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal"/>
</VisualStateGroup>
<VisualStateGroup x:Name="SelectionStates">
<VisualState x:Name="Unselected">
<Storyboard>
<ColorAnimation Duration="0" Storyboard.TargetName="myback" Storyboard.TargetProperty="(Border.Background).(SolidColorBrush.Color)" To="Transparent"/>
</Storyboard>
</VisualState>
<VisualState x:Name="SelectedUnfocused">
<Storyboard>
<ColorAnimation Duration="0" Storyboard.TargetName="myback" Storyboard.TargetProperty="(Border.Background).(SolidColorBrush.Color)" To="Red"/>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Border x:Name="myback" Background="Transparent">
<ContentPresenter Content="{TemplateBinding Content}" ContentTemplate="{TemplateBinding ContentTemplate}"/>
</Border>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ListView.ItemContainerStyle>
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel Height="100">
<TextBlock Text="{Binding Artist}" FontSize="22"/>
<TextBlock Text="{Binding Song}" FontSize="22"/>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
C#(样本模型)
public class sample_data
{
public sample_data(string artist, string song)
{
this.Artist = artist;
this.Song = song;
}
public string Artist { get; set; }
public string Song { get; set; }
}
private ObservableCollection<sample_data> CreateData()
{
//List<sample_data> my_list = new List<sample_data>();
ObservableCollection<sample_data> my_list = new ObservableCollection<sample_data>();
my_list.Add(new sample_data("Faith + 1", "Body of Christ"));
my_list.Add(new sample_data("Faith + 1", "Christ Again"));
my_list.Add(new sample_data("Faith + 1", "A Night With the Lord"));
my_list.Add(new sample_data("Faith + 1", "Touch Me Jesus"));
my_list.Add(new sample_data("Faith + 1", "I Found Jesus (With Someone Else)"));
my_list.Add(new sample_data("Faith + 1", "Savior Self"));
my_list.Add(new sample_data("Faith + 1", "Christ What a Day"));
my_list.Add(new sample_data("Faith + 1", "Three Times My Savior"));
my_list.Add(new sample_data("Faith + 1", "Jesus Touched Me"));
my_list.Add(new sample_data("Faith + 1", "Lord is my Savior"));
my_list.Add(new sample_data("Faith + 1", "I Wasn't Born Again Yesterday"));
my_list.Add(new sample_data("Faith + 1", "Pleasing Jesus"));
my_list.Add(new sample_data("Faith + 1", "Jesus (Looks Kinda Hot)"));
my_list.Add(new sample_data("Butters", "What What"));
return my_list;
}
private void Page_Loaded(object sender, RoutedEventArgs e)
{
ObservableCollection<sample_data> sd = this.CreateData();
mylistview.ItemsSource = sd;
}
正在运行的屏幕截图:
答案 1 :(得分:0)
您需要为ListBoxItem创建一个样式并使用故事板。
以下是一个示例:
<Page.Resources>
<Style x:Key="ListViewItemTemplate" TargetType="ListViewItem">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListViewItem">
<Border x:Name="LayoutRoot">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="SelectionStates">
<VisualState x:Name="Unselected">
<Storyboard>
<!-- Define style -->
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="container" Storyboard.TargetProperty="Background">
<DiscreteObjectKeyFrame KeyTime="0" Value="Transparent"/>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
<VisualState x:Name="Selected">
<Storyboard>
<!-- Define style -->
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="container" Storyboard.TargetProperty="Background">
<DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneAccentBrush}"/>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Grid Margin="0,5" x:Name="container" Background="Transparent">
<!-- Definition of your list item. -->
</Grid>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
列表视图的定义:
<ListView ItemContainerStyle="{StaticResource ListViewItemTemplate}" SelectionMode="Single" />
答案 2 :(得分:0)
是的,您需要在所选属性上设置带触发器的样式。我的手机代码很难用yype,但快速谷歌会向你显示大量的例子或在这里:ListBox Style Selected item on windows phone
答案 3 :(得分:0)
您可以向类中添加一个布尔变量IsSelected,并将其转换为bg颜色。例如:
<DataTemplate>
<Grid Background="{Binding IsSelected, Converter={StaticResource IsSelectedToBackgroundColorConverter}}">
<TextBlock Text="{Binding displayName}"
Style="{ThemeResource ListViewItemTextBlockStyle}" />
</Grid>
</DataTemplate>
|
class IsSelectedToBackgroundColorConverter: IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string language)
{
bool IsSelected = (bool)value;
if (IsSelected)
{
Windows.UI.Xaml.Media.Brush red = new SolidColorBrush(Windows.UI.Colors.Red);
return red;
}
else
{
Windows.UI.Xaml.Media.Brush transparent = new SolidColorBrush(Windows.UI.Colors.Transparent);
return transparent;
}
}
}
答案 4 :(得分:0)
您只需将以下内容添加到App.xaml
即可 </Application.Resources>
<SolidColorBrush x:Key="ListViewItemSelectedBackgroundThemeBrush" Color="#92D050" />
<Application.Resources>
“hex-Color”将是应用程序范围内ListView中的选定项目颜色
答案 5 :(得分:0)
试试这个,希望它可以帮到你。
if (e.AddedItems != null) {
foreach (var item in e.AddedItems) {
ListViewItem litem = (sender as ListView).ContainerFromItem(item) as ListViewItem;
if (litem != null) {
VisualStateManager.GoToState(litem, "Unfocused", true);
VisualStateManager.GoToState(litem, "Normal", true);
VisualStateManager.GoToState(litem, "Selected", true);
}
}
}
if (e.RemovedItems != null) {
foreach (var item in e.RemovedItems) {
ListViewItem litem = (sender as ListView).ContainerFromItem(item) as ListViewItem;
if (litem != null) {
VisualStateManager.GoToState(litem, "Unselected", true);
}
}
}