2011-03-15 76 views
1

我开始在触摸设备上进行WPF开发。虽然.NET和WPF似乎是一个了不起的技术,但我有点迷路。WPF:风格ListBoxItem统一颜色

我正在开发一个通用控件。我定义了一个UserControl,它包含一个Grid,一些按钮和一个SurfaceListBox。在实际的C#代码中,我正在处理事件,并通过操作listItems-Attribute将新元素添加到列表框中。迄今为止,这工作得很好。

现在我想改变列表项的样式 - 如果它们没有被选中,我想让它们的背景透明,如果它们是完全白色的。不幸的是,下面的一段代码根本无法工作。它只是将列表项文本的背景颜色设置为白色,但不是整个项目。

更新:现在工作!

<UserControl x:Class="SGEditor.MyControl" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     mc:Ignorable="d" 
     xmlns:my="http://schemas.microsoft.com/surface/2008" UseLayoutRounding="True"> 
<UserControl.Resources> 
    <Style TargetType="{x:Type ListBoxItem}" x:Key="ContainerStyle"> 
     <Setter Property="Background" Value="Transparent" /> 
     <Style.Triggers> 
      <Trigger Property="IsSelected" Value="True"> 
       <Setter Property="Background" Value="White" /> 
      </Trigger> 
     </Style.Triggers> 
    </Style> 
</UserControl.Resources> 

<Grid>   
    <my:SurfaceListBox Width="300" Height="300" /> 
</Grid> 
</UserControl> 

谢谢!

汉斯

回答

1

我不知道到底该怎么做你想做的事,但我敢肯定像你想在x:Key做你不能重新分配一个静态的制度价值。这只是一个名称,用于稍后在xaml中引用资源,如MyBackgroundColor或其他。

我认为你需要做的是为你的控件设置适合你要改变的元素的属性的触发器。希望别人能够更多地了解这一点,因为我很少做那些奇特的事情。 =)

+0

嘿,谢谢。更新我的问题;) – HansSchild 2011-03-15 21:16:28

+0

好吧,明白了。谢谢你,小伙伴! – HansSchild 2011-03-15 21:32:44