我有一些wpf的来源:
<Window x:Class="WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<SolidColorBrush x:Key="RedBrush" Color="Red"/>
<SolidColorBrush x:Key="GreenBrush" Color="Green"/>
<SolidColorBrush x:Key="TransparentBrush" Color="Transparent"/>
<DataTemplate x:Key="MyItemTemplate">
<Grid Margin="5">
<Grid.ColumnDefinitions>
<ColumnDefinition />
</Grid.ColumnDefinitions>
<TextBlock Background="{DynamicResource TransparentBrush}">
<TextBlock.Style>
<Style>
<Style.Triggers>
<DataTrigger Binding="{Binding State}" Value="Stat1">
<Setter Property="TextBlock.Background" Value="{DynamicResource RedBrush}" />
</DataTrigger>
<DataTrigger Binding="{Binding State}" Value="Stat2">
<DataTrigger.EnterActions>
<BeginStoryboard>
<Storyboard RepeatBehavior="Forever">
<ColorAnimation
Storyboard.TargetProperty="(TextBlock.Background).(SolidColorBrush.Color)"
Duration="00:00:01"
From="Yellow" To="Red"/>
</Storyboard>
</BeginStoryboard>
</DataTrigger.EnterActions>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
<TextBlock.Text>
<Binding Path="Name" />
</TextBlock.Text>
</TextBlock>
</Grid>
</DataTemplate>
</Window.Resources>
<ListBox x:Name="SomeList"
HorizontalContentAlignment="Stretch"
VerticalContentAlignment="Top"
ScrollViewer.HorizontalScrollBarVisibility="Disabled"
ScrollViewer.VerticalScrollBarVisibility="Visible"
ItemTemplate="{StaticResource MyItemTemplate}">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid Columns="3" />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
</ListBox>
我在名单上填上:
public MainWindow()
{
InitializeComponent();
myList_ = new List<Foo>();
myList_.Add(new Foo() { State = "Stat1", Name = "Name 1" });
myList_.Add(new Foo() { State = "Stat2", Name = "Name 2" });
SomeList.ItemsSource = myList_;
}
问题是,我有个例外
“Background”属性未指向路径“(0)中的DependencyObject。(1)”。
我不知道为什么:-(安迪博迪知道吗?
谢谢
当做
迈克尔
最佳答案
您使用DynamicResource
而不是StaticResource
有什么特别的原因吗?据我所知,只有在动态更新样式时才应该使用DynamicResource
。
如果我在DynamicResource
中更改StaticResource
,则代码对我有效。
编辑:这是对我有用的代码:
<Window x:Class="BackgroundTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<SolidColorBrush x:Key="RedBrush" Color="Red"/>
<SolidColorBrush x:Key="GreenBrush" Color="Green"/>
<SolidColorBrush x:Key="TransparentBrush" Color="Transparent"/>
<DataTemplate x:Key="MyItemTemplate">
<Grid Margin="5">
<Grid.ColumnDefinitions>
<ColumnDefinition />
</Grid.ColumnDefinitions>
<TextBlock Background="{StaticResource TransparentBrush}">
<TextBlock.Style>
<Style>
<Style.Triggers>
<DataTrigger Binding="{Binding State}" Value="Stat1">
<Setter Property="TextBlock.Background" Value="{StaticResource RedBrush}" />
</DataTrigger>
<DataTrigger Binding="{Binding State}" Value="Stat2">
<DataTrigger.EnterActions>
<BeginStoryboard>
<Storyboard RepeatBehavior="Forever">
<ColorAnimation
Storyboard.TargetProperty="(TextBlock.Background).(SolidColorBrush.Color)"
Duration="00:00:01"
From="Yellow" To="Red"/>
</Storyboard>
</BeginStoryboard>
</DataTrigger.EnterActions>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
<TextBlock.Text>
<Binding Path="Name" />
</TextBlock.Text>
</TextBlock>
</Grid>
</DataTemplate>
</Window.Resources>
<ListBox x:Name="SomeList"
HorizontalContentAlignment="Stretch"
VerticalContentAlignment="Top"
ScrollViewer.HorizontalScrollBarVisibility="Disabled"
ScrollViewer.VerticalScrollBarVisibility="Visible"
ItemTemplate="{StaticResource MyItemTemplate}">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid Columns="3" />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
</ListBox>
以及我的
MainWindow.xaml.cs
:public partial class MainWindow : Window
{
IList<Foo> myList_;
public MainWindow()
{
InitializeComponent();
myList_ = new List<Foo>();
myList_.Add(new Foo() { State = "Stat1", Name = "Name 1" });
myList_.Add(new Foo() { State = "Stat2", Name = "Name 2" });
SomeList.ItemsSource = myList_;
}
}