Frage

I have the following DataTemplate and I am trying to set the ItemsSource property of the ComboBox on the basis of a bound field (parent).

I have the trigger in DataTemplate.Triggers however this cannot see the CollectionViewSources in Grid.Resouces

If I try and put the triggers in Grid.Triggers then the controls themselves cannot be seen.

If I put the CollectionViewSources in DataTemplate.Resouces then it can't find the collections in the code behind (as I don't think the DataTemplate is part of the tree where as the grid is??)

Help! Thanks Andy

        <!-- 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>
War es hilfreich?

Lösung

Found the answer....

            <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>

and remove the Triggers from the DataTemplate.Triggers section

Andy

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top