DataTemplate触发器无法访问包含Grid资源(CollectionViewSource)

时间:2014-05-13 00:43:43

标签: wpf xaml triggers grid datatemplate

我有以下DataTemplate,我正在尝试根据绑定字段(父级)设置ComboBox的ItemsSource属性。

我在DataTemplate.Triggers中有触发器,但是CollectionViewSources无法看到Grid.Resouces

如果我尝试将触发器放在Grid.Triggers中,则无法看到控件本身。

如果我将CollectionViewSources放在DataTemplate.Resouces中,则无法在后面的代码中找到集合(因为我不认为DataTemplate是树的一部分,因为网格是??)

帮助! 谢谢 安迪

        <!-- Template for each child item in ListView -->
    <DataTemplate x:Key="ChildTemplate">            
        <Grid>
            <Grid.ColumnDefinitions>                    
                <ColumnDefinition Width="90"/>                    
                <ColumnDefinition Width="130"/>
                <ColumnDefinition Width="75"/>
                <ColumnDefinition Width="90"/>
                <ColumnDefinition Width="90"/>
                <ColumnDefinition Width="160"/>
                <ColumnDefinition Width="90"/>
                <ColumnDefinition Width="210"/>
                <ColumnDefinition Width="55"/>
                <ColumnDefinition Width="55"/>
                <ColumnDefinition Width="55"/>
            </Grid.ColumnDefinitions>
            <Grid.Resources>
                <CollectionViewSource x:Key="RealData" Source="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type l:ItemView}}, Path=RelationRealAccounts}">
                    <CollectionViewSource.GroupDescriptions>
                        <PropertyGroupDescription PropertyName="group"/>
                    </CollectionViewSource.GroupDescriptions>
                </CollectionViewSource>
                <CollectionViewSource x:Key="VirtualData" Source="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type l:ItemView}}, Path=RelationVirtualAccounts}">
                    <CollectionViewSource.GroupDescriptions>
                        <PropertyGroupDescription PropertyName="group"/>
                    </CollectionViewSource.GroupDescriptions>
                </CollectionViewSource>
                <CollectionViewSource x:Key="ChildCategories" Source="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type l:ItemView}}, Path=Categories}"/>
            </Grid.Resources>                
            <Label Grid.Column="0" Content="Account" HorizontalAlignment="Left" VerticalAlignment="Top" Foreground="{StaticResource CustomWhite}" FontSize="14" Width="80"/>
            <ComboBox x:Name="cboChildAccount" Grid.Column="1" SelectedValue="{Binding Path=accFrom, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" SelectedValuePath="ID" ItemTemplate="{StaticResource AccountTemplate}" Width="120" Style="{StaticResource RoundedComboBox}" HorizontalAlignment="Left" VerticalAlignment="Center" IsTextSearchEnabled="True" TextSearch.TextPath="comment" IsSynchronizedWithCurrentItem="False" SelectionChanged="cboChangedC">
                <ComboBox.GroupStyle>
                    <GroupStyle ContainerStyle="{StaticResource CustomExpanderComboGroupItemStyle}" HeaderTemplate="{StaticResource GroupHeader}"/>
                </ComboBox.GroupStyle>
            </ComboBox>
            <Label Grid.Column="2" Content="Amount" HorizontalAlignment="Left" VerticalAlignment="Top" Foreground="{StaticResource CustomWhite}" FontSize="14" Width="65"/>
            <TextBox x:Name="txtAmountChild" Grid.Column="3" Style="{StaticResource RoundedTextBox}" Width="80" HorizontalAlignment="Left" VerticalAlignment="Center" LostFocus="txtLostFocusChild" Validation.ErrorTemplate="{StaticResource ValidationTemplate}">
                <TextBox.Text>
                    <Binding Path="amount" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged" StringFormat="#,##0.00">
                        <Binding.ValidationRules>
                            <l:CurrencyRule/>
                        </Binding.ValidationRules>
                    </Binding>
                </TextBox.Text>
            </TextBox>
            <Label Grid.Column="4" Content="Category" HorizontalAlignment="Left" VerticalAlignment="Top" Foreground="{StaticResource CustomWhite}" FontSize="14" Width="80"/>
            <ComboBox Grid.Column="5" x:Name="cboCategoryChild" ItemsSource="{Binding Source={StaticResource ChildCategories}}" ItemTemplate="{StaticResource CategoryTemplate}" SelectedValue="{Binding Path=category, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" SelectedValuePath="ID" Width="150" Style="{StaticResource RoundedComboBox}" HorizontalAlignment="Left" VerticalAlignment="Center" IsTextSearchEnabled="True" TextSearch.TextPath="comment" IsSynchronizedWithCurrentItem="False" PreviewKeyDown="cboPreviewKeyDown"/>                
            <Label Grid.Column="6" Content="Comment" HorizontalAlignment="Left" VerticalAlignment="Top" Foreground="{StaticResource CustomWhite}" FontSize="14" Width="80"/>
            <TextBox Grid.Column="7" Text="{Binding Path=comment, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Style="{StaticResource RoundedTextBox}" Width="200" HorizontalAlignment="Left" VerticalAlignment="Center" MaxLength="50"/>
            <Button x:Name="btnEdit" Grid.Column="8" Content="Edit" HorizontalAlignment="Left" Height="21" VerticalAlignment="Center" Width="45" Style="{StaticResource RoundedButtonStyle}" Click="btnEditClick"/>            
            <Button Grid.Column="9" Content="Delete" HorizontalAlignment="Left" Height="21" VerticalAlignment="Center" Width="45" Style="{StaticResource RoundedButtonStyle}" Click="btnDeleteClick"/>
            <Button Grid.Column="10" Content="Fill" HorizontalAlignment="Left" Height="21" VerticalAlignment="Center" Width="45" Style="{StaticResource RoundedButtonStyle}" Click="btnFillClick"/>                
        </Grid>            
        <DataTemplate.Triggers>                
            <DataTrigger Binding="{Binding Path=accFrom}" Value="{x:Null}">                    
                <Setter TargetName="btnEdit" Property="IsEnabled" Value="False"/>
            </DataTrigger>
            <DataTrigger Binding="{Binding Path=parent, Converter={StaticResource IsNullConverter}}" Value="True">
                <Setter TargetName="cboChildAccount" Property="ItemsSource" Value="{Binding Source={StaticResource RealData}}"/>
            </DataTrigger>
            <DataTrigger Binding="{Binding Path=parent, Converter={StaticResource IsNullConverter}}" Value="False">
                <Setter TargetName="cboChildAccount" Property="ItemsSource" Value="{Binding Source={StaticResource VirtualData}}"/>
            </DataTrigger>
        </DataTemplate.Triggers>
    </DataTemplate>

1 个答案:

答案 0 :(得分:0)

找到答案......

            <ComboBox x:Name="cboChildAccount" Grid.Column="1" SelectedValue="{Binding Path=accFrom, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" SelectedValuePath="ID" ItemTemplate="{StaticResource AccountTemplate}" Width="120" HorizontalAlignment="Left" VerticalAlignment="Center" IsTextSearchEnabled="True" TextSearch.TextPath="comment" IsSynchronizedWithCurrentItem="False" SelectionChanged="cboChangedC">
                <ComboBox.GroupStyle>
                    <GroupStyle ContainerStyle="{StaticResource CustomExpanderComboGroupItemStyle}" HeaderTemplate="{StaticResource GroupHeader}"/>
                </ComboBox.GroupStyle>
                <ComboBox.Style>
                    <Style TargetType="ComboBox" BasedOn="{StaticResource RoundedComboBox}">
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding Path=parent, Converter={StaticResource IsNullConverter}}" Value="True">
                                <Setter Property="ItemsSource" Value="{Binding Source={StaticResource RealData}}"/>
                            </DataTrigger>
                            <DataTrigger Binding="{Binding Path=parent, Converter={StaticResource IsNullConverter}}" Value="False">
                                <Setter Property="ItemsSource" Value="{Binding Source={StaticResource VirtualData}}"/>
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </ComboBox.Style>
            </ComboBox>

并从DataTemplate.Triggers部分

中删除触发器

安迪