2010-01-13 143 views
1

我想申请一个DataTrigger改变DataTemplateListBox和正在错误如何改变的ListBox的DataTemplate WPF中:基于复选框

"Error 1 Cannot find the Trigger target 'IssueListBox'. (The target must appear before any Setters, Triggers, or Conditions that use it.)"

我在主体WindowListBox (在DockPanel连同其他控制):

<ListBox x:Name="IssueListBox" 
    ItemsSource="{Binding}" 
    ItemTemplate="{StaticResource ShowIssueSimple}" 
    IsSynchronizedWithCurrentItem="True" 
    HorizontalContentAlignment="Stretch" 
    BorderThickness="3" DockPanel.Dock="Top" 
    VerticalContentAlignment="Stretch" Margin="2"/> 

我有对DataTemplate个S IN的App.xaml有DataTrigger在第2模板的底部:

<DataTemplate x:Key="ShowIssueDetail"> 
     <Border CornerRadius="4, 8, 4, 8" Margin="2" MinWidth="400" BorderThickness="3" 
       BorderBrush="{Binding Path=IssUrgency, Converter={StaticResource IntToRYGBBoarderBrushConverter}}"> 
      <StackPanel Orientation="Horizontal"> 
       <StackPanel Margin="10"> 
        <TextBlock Text="{Binding IssSubject}" FontWeight="Bold" FontSize="14"/> 
        <StackPanel Width="Auto" Orientation="Horizontal"> 
         <TextBlock Text="Due: " FontWeight="Bold"/> 
         <TextBlock Text="{Binding IssDueDate}" FontStyle="Italic" HorizontalAlignment="Left"/> 
        </StackPanel> 
        <StackPanel Width="Auto" Orientation="Horizontal"> 
         <TextBlock Text="Category: " FontWeight="Bold"/> 
         <TextBlock Text="{Binding IssCategory}"/> 
        </StackPanel> 
       </StackPanel> 
      </StackPanel> 
     </Border> 
    </DataTemplate> 

    <DataTemplate x:Key="ShowIssueSimple"> 

     <Border CornerRadius="6" 
       Margin="2,1,2,1" 
       MinWidth="400" 
       BorderThickness="2" 
       SnapsToDevicePixels="True" 
       BorderBrush="{Binding Path=IssUrgency, Converter={StaticResource IntToRYGBBoarderBrushConverter}}"> 
      <StackPanel Margin="5"> 
       <TextBlock Text="{Binding IssSubject}" FontWeight="Bold" FontSize="14"/> 
      </StackPanel> 
     </Border> 
     <DataTemplate.Triggers> 
      <DataTrigger Binding="{Binding Source={StaticResource sbvm}, Path=ShowDetailListItems}" Value="True"> 
       <Setter TargetName="IssueListBox" Property="ItemTemplate" Value="{StaticResource ShowIssueDetail}"/> 
      </DataTrigger> 
     </DataTemplate.Triggers> 
    </DataTemplate> 

如何获得Trigger工作?谷歌先生让我失望了,像这样的例子很多,但他们不是基于另一种控制。

+0

在DataTrigger中,您将ItemTemplate设置为包含它的DataTemplate - ShowIssueSimple。这只是一个错字吗?我猜你的意思是Value =“{StaticResource ShowIssueDetail}”。 – Tarsier 2010-01-13 03:32:25

+0

是的,是的一个“错字”(: 感谢您的领导,修正 – 2010-01-13 03:38:44

回答

3

你的数据模板是一个在app.xaml中定义的StaticResource,你试图做一个元素名绑定到不存在于同一个作用域中的IssueListBox元素。即使如此,你想要做的是这个。 Listbox有一个数据模板DT,在DT内部,您试图返回到列表框并将其DataTemplate设置为另一个(而不是DT)。

为什么不合并模板,将详细信息的可见性设置为折叠并根据您的属性触发可见性。那么你根本不必引用列表框,模板也保持不变,当你想看到细节时,它只是在内部改变。

+1

虽然我仍然想知道如何处理它,如果我需要两个模板,当我刚刚添加时,生活是如此容易事件处理程序的checked.changed事件,但我正在努力开发更好的习惯,因为我正在学习和做学习MVVMish,因为我可以学习xaml – 2010-01-13 14:53:10

+1

您可以使用数据模板选择器,但这是要决定数据模板当他们被添加时,而不是当属性改变时。 – 2010-01-14 00:09:16