WPF嵌套ItemsControl绑定问题

时间:2017-06-29 05:31:06

标签: wpf

为什么绑定到FirstName的第二个ItemsControl不起作用?

 <ItemsControl Grid.Row="1" x:Name="PMItmsControl" VerticalAlignment="Top" ItemsSource="{Binding FirstEntityCollection}">
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <VirtualizingStackPanel Orientation="Vertical"/>
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <Border CornerRadius="5" BorderBrush="YellowGreen" BorderThickness="1" Margin="5">
                        <Grid Width="auto" Height="100" VerticalAlignment="Top">
                            <Grid.RowDefinitions>
                                <RowDefinition/>
                            </Grid.RowDefinitions>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="60*"/>
                                <ColumnDefinition Width="40*"/>
                            </Grid.ColumnDefinitions>
                            <TextBlock **x:Name="tb"** Width="100" Foreground="White" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="10" Text="{Binding FirstName}" FontSize="10" >
                            </TextBlock>
                            <ItemsControl ItemsSource="{Binding SecondEntityCollection}" Grid.Column="1" VerticalAlignment="Top">
                                <ItemsControl.ItemsPanel>
                                    <ItemsPanelTemplate>
                                        <VirtualizingStackPanel Orientation="Vertical"/>
                                    </ItemsPanelTemplate>
                                </ItemsControl.ItemsPanel>
                                <ItemsControl.ItemTemplate>
                                    <DataTemplate>
                                        <Grid>
                                            <Grid.ColumnDefinitions>
                                                <ColumnDefinition Width="50*"/>
                                                <ColumnDefinition Width="50*"/>
                                            </Grid.ColumnDefinitions>
                                            <TextBlock Text="{Binding SecondName}" Margin="5"  Foreground="White" />
                                            <TextBlock Grid.Column="1" Text="{Binding **Path= Text, ElementName=tb**}" Margin="5" Width="40">
                                            </TextBlock>
                                        </Grid>
                                    </DataTemplate>
                                </ItemsControl.ItemTemplate>
                            </ItemsControl>
                        </Grid>
                    </Border>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>

在我的代码后面,我声明了以下类:

public class FirstEntity 
    {
        public string FirstName { get; set; }
        public ObservableCollection<SecondEntity> SecondEntityCollection { get; set; }
    }

    public class SecondEntity
    {
        public string SecondName { get; set; }
    }

并且我的代码后面的FirstEntityCollection声明如下:

public ObservableCollection<FirstEntity> FirstEntityCollection {get;set;}

不确定我的问题是否清楚? 有人能给我任何想法吗?请。

非常感谢。非常感谢。

更新:我刚发现另一个解决方案使用x:Name =&#34; tb&#34;以上粗体代码。然后更改Text =&#34; {Binding Path = DataContext.FirstName,RelativeSource = {RelativeSource Mode = FindAncestor,AncestorType = {x:Type ItemsControl},AncestorLevel = 2}}&#34; to Text =&#34; {Binding Text,ElementName = tb}&#34;

谢谢Maxi。

----------------再次更新------------- 这是我第一次在stackoverflow中提问,问题编辑器输入代码和注释并不容易,格式化也很糟糕,也许我找不到一个简单易用的方法。无论如何,谢谢。

1 个答案:

答案 0 :(得分:0)

您需要摆脱的第一个陷害是在Foreground="White"上设置TextBlock

根据您的问题更新,请在下面找到XAML绑定。

        <ItemsControl Grid.Row="1" x:Name="PMItmsControl" VerticalAlignment="Top" ItemsSource="{Binding FirstEntityCollection}">
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <VirtualizingStackPanel Orientation="Vertical"/>
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <Border CornerRadius="5" BorderBrush="YellowGreen" BorderThickness="1" Margin="5">
                    <Grid Width="auto" Height="100" VerticalAlignment="Top">
                        <Grid.RowDefinitions>
                            <RowDefinition/>
                        </Grid.RowDefinitions>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="60*"/>
                            <ColumnDefinition Width="40*"/>
                        </Grid.ColumnDefinitions>
                        <TextBlock Width="100" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="10" Text="{Binding FirstName}" FontSize="10" >
                        </TextBlock>
                        <ItemsControl ItemsSource="{Binding SecondEntityCollection}" Grid.Column="1" VerticalAlignment="Top">
                            <ItemsControl.ItemsPanel>
                                <ItemsPanelTemplate>
                                    <VirtualizingStackPanel Orientation="Vertical"/>
                                </ItemsPanelTemplate>
                            </ItemsControl.ItemsPanel>
                            <ItemsControl.ItemTemplate>
                                <DataTemplate>
                                    <Grid>
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="50*"/>
                                            <ColumnDefinition Width="50*"/>
                                        </Grid.ColumnDefinitions>
                                        <TextBlock Text="{Binding SecondName}" Margin="5"  />
                                        <!--Notice setting DataContext to TextBlock below-->
                                        <TextBlock Grid.Column="1" DataContext="{Binding Path=DataContext, RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type ItemsControl}}}" Text="{Binding Path= FirstName}" Margin="5" Width="Auto"/>                                        </Grid>
                                </DataTemplate>
                            </ItemsControl.ItemTemplate>
                        </ItemsControl>
                    </Grid>
                </Border>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>

希望这能给你一个开头的想法......