C# 单个listviewitems selected、Pointer Over和pressed状态是否可以由数据绑定项控制
我目前正在尝试设计一个C# 单个listviewitems selected、Pointer Over和pressed状态是否可以由数据绑定项控制,c#,xaml,uwp,uwp-xaml,C#,Xaml,Uwp,Uwp Xaml,我目前正在尝试设计一个列表视图,以便其中的每个项目都可以控制自己的背景色、指针、按下和选定状态 我发现我可以将ListViewItem的背景设置为Transparent,并让我的项目的DataTemplate控制背景。但我正在努力解决的是ListViewItem的VisualStates。他们只是看起来风格不正确 到目前为止,我尝试的是将我的绑定代码放在样式的列表视图项中,如下所示: 注意:为了简洁起见,代码已被缩减,ListViewItem样式取自Generic.xaml <Style
列表视图
,以便其中的每个项目都可以控制自己的背景色、指针、按下和选定状态
我发现我可以将ListViewItem
的背景设置为Transparent
,并让我的项目的DataTemplate
控制背景。但我正在努力解决的是ListViewItem
的VisualStates
。他们只是看起来风格不正确
到目前为止,我尝试的是将我的绑定
代码放在样式
的列表视图项
中,如下所示:
注意:为了简洁起见,代码已被缩减,ListViewItem
样式取自Generic.xaml
<Style TargetType="ListViewItem">
<Setters.../>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListViewItem">
<Grid x:Name="ContentBorder">
<!-- ContentPresenter etc in here -->
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Pressed">
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter" Storyboard.TargetProperty="Background">
<DiscreteObjectKeyFrame KeyTime="0" Value="{Binding AccentColour}" />
</ObjectAnimationUsingKeyFrames>
</VisualState>
<!-- More Visual States-->
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
我考虑过的另一种方法是为ListViewItem上的颜色创建AttachedProperties
。唯一的问题是我不确定我将如何与他们结合
任何人都有任何想法,如果它是有可能的项目,以这种方式控制其状态,任何帮助表示感谢 实现一个为给定项选择所需ListViewItem样式的,并将其设置为ListView上的样式
我考虑过的另一种方法是为创建AttachedProperties
ListViewItem上的颜色。唯一的问题是我不知道我会怎么做
约束他们
关于使用AttachedProperties,您是对的,这就是如何做到的
第1步。定义附加属性
public static Brush GetPointerOverBackground(DependencyObject obj)
{
return (Brush)obj.GetValue(PointerOverBackgroundProperty);
}
public static void SetPointerOverBackground(DependencyObject obj, Brush value)
{
obj.SetValue(PointerOverBackgroundProperty, value);
}
public static readonly DependencyProperty PointerOverBackgroundProperty =
DependencyProperty.RegisterAttached("PointerOverBackground", typeof(Brush), typeof(YourStaticClass), new PropertyMetadata(null));
<Setter Property="local:YourStaticClass.PointerOverBackground" Value="LightBlue" />
<VisualState x:Name="PointerOver">
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter" Storyboard.TargetProperty="Background">
<DiscreteObjectKeyFrame KeyTime="0" Value="{TemplateBinding local:YourStaticClass.PointerOverBackground}" />
</ObjectAnimationUsingKeyFrames>
</VisualState>
第2步。定义附加属性的默认值
public static Brush GetPointerOverBackground(DependencyObject obj)
{
return (Brush)obj.GetValue(PointerOverBackgroundProperty);
}
public static void SetPointerOverBackground(DependencyObject obj, Brush value)
{
obj.SetValue(PointerOverBackgroundProperty, value);
}
public static readonly DependencyProperty PointerOverBackgroundProperty =
DependencyProperty.RegisterAttached("PointerOverBackground", typeof(Brush), typeof(YourStaticClass), new PropertyMetadata(null));
<Setter Property="local:YourStaticClass.PointerOverBackground" Value="LightBlue" />
<VisualState x:Name="PointerOver">
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter" Storyboard.TargetProperty="Background">
<DiscreteObjectKeyFrame KeyTime="0" Value="{TemplateBinding local:YourStaticClass.PointerOverBackground}" />
</ObjectAnimationUsingKeyFrames>
</VisualState>
第5步。创建一个行为
负责将画笔
es分配给列表视图项
s
public static Brush GetPointerOverBackground(DependencyObject obj)
{
return (Brush)obj.GetValue(PointerOverBackgroundProperty);
}
public static void SetPointerOverBackground(DependencyObject obj, Brush value)
{
obj.SetValue(PointerOverBackgroundProperty, value);
}
public static readonly DependencyProperty PointerOverBackgroundProperty =
DependencyProperty.RegisterAttached("PointerOverBackground", typeof(Brush), typeof(YourStaticClass), new PropertyMetadata(null));
<Setter Property="local:YourStaticClass.PointerOverBackground" Value="LightBlue" />
<VisualState x:Name="PointerOver">
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter" Storyboard.TargetProperty="Background">
<DiscreteObjectKeyFrame KeyTime="0" Value="{TemplateBinding local:YourStaticClass.PointerOverBackground}" />
</ObjectAnimationUsingKeyFrames>
</VisualState>
这就是一切结合在一起的地方。首先,我们需要安装。然后,创建一个名为SupportListViewItemBrushesBehavior
的行为
,并使其仅可附加到列表视图
public class SupportListViewItemBrushesBehavior : Behavior<ListView>
就这些
注意,我们必须在这里创建
行为
,因为UWP还不支持FindAncestor
绑定。或者,您可以扩展列表视图
/列表视图项
,而不是使用附加属性和行为
,以获得相同的结果
我附上了一个小样品供你参考。这是最终结果。希望这有帮助
您是直接使用数据模板还是ListViewItem?两者的组合。我使用DataTemplates将类中的信息放入项目中。然后,我一直在使用ListViewItem来设置状态的样式,例如pointerover、pressed和selected。我不能这样做,不幸的是,这些项目是通过Web服务同步的。这是为了允许通过web界面更改项目颜色,也可以随时添加新颜色。为添加的每一种新颜色创建一种样式意味着必须发布新版本的应用程序,以便用户可以看到新颜色。我知道必须有一些方法通过AttachedProperties来实现,但我无法理解。非常感谢,这太棒了