【问题标题】:WPF Binding to a sub-collectionWPF 绑定到子集合
【发布时间】:2016-08-12 23:42:32
【问题描述】:

我在 WPF 中定义了一个 Grid 控件...

<Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="Auto" />
        <RowDefinition Height="*" />
        <RowDefinition Height="Auto" />
        <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*" />
        <ColumnDefinition Width="*" />
    </Grid.ColumnDefinitions>

再往下,我定义了一个 ItemsControl...

 <ItemsControl Name="EntitlementsList" Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="2" HorizontalAlignment="Stretch"
                  VerticalAlignment="Stretch"
                  ItemsSource="{Binding Entitlements, Mode=TwoWay}"
                  Margin="0 10 0 3" AlternationCount="2">
        <ItemsControl.Template>
            ...
        </ItemsControl.Template>
        <ItemsControl.ItemTemplate>
            <DataTemplate DataType="{x:Type Model:Entitlement}">
                <Grid Margin="0 0 10 0" >
                    <Grid.Style>
                      ...
                    </Grid.Style>
                    <Grid.ColumnDefinitions>
                        ...
                    </Grid.ColumnDefinitions>
                    <ToggleButton Grid.Column="0"
                                  HorizontalAlignment="Left" Margin="0 2 0 0"
                                  VerticalAlignment="Bottom" 
                                  Style="{DynamicResource NotesToggleButton}"
                                  CommandParameter="{Binding}"
                                  Command="{Binding Path=DataContext.GetEntitlementDetails, 
                                            RelativeSource={RelativeSource FindAncestor, 
                                            AncestorType={x:Type ItemsControl}}}" />
                ...
                </Grid>
             </DataTemplate>
        </ItemsControl.ItemTemplate>
</ItemsControl>

这会正确显示我的权利。

ToggleButton 的命令定义为...

private RelayCommand _getEntitlementDetails;
public RelayCommand GetEntitlementDetails
{
    get
    {
        return _getEntitlementDetails ?? (_getEntitlementDetails = new RelayCommand(x =>
        {
            CurrentEntitlement = x as Entitlement;
        }));
    }
}

然后我有另一个 ItemsControl 在设置 CurrentEntitlement 之前折叠...

<ItemsControl Name="ProductList" Grid.Row="3" Grid.Column="0" Grid.ColumnSpan="5" Height="150"
              HorizontalAlignment="Stretch" BorderBrush="Transparent"
                  ItemsSource="{Binding ContextManager.CurrentContext.CurrentEntitlement.ProductList, Mode=TwoWay}" 
                  Visibility="{Binding ContextManager.CurrentContext.CurrentEntitlement, 
                                                Converter={StaticResource NullVisibilityConverter}}">
        <ItemsControl.Template>
        ...
</ItemsControl>

我的项目来源是 Entitlements 类中的 ProductList...

private ObservableCollection<Product> _productList;
public ObservableCollection<Product> ProductList
{
    get { return _productList; }
    set
    {
        _productList = value;
        OnPropertyChanged();
    }
}

...“Product”的所有属性都会引发 OnPropertyChanged。

现在,我一直期待我的按钮执行设置 CurrentEntitlement 的命令,使我的第二个 ItemsControl 在网格中可见并显示 ProductList,并且单步执行代码显示 CurrentEntitlement 确实设置了一个有效的实例ProductList 中的项目...但我仍然在 UI 上看不到任何内容。我一直在寻找解决方案 3 天,如果这是一个重复的问题,我深表歉意,但我无能为力!

我看不到问题。我在这里做错了什么?

Visual Studio 2015 Community,面向 4.5,在 Windows 7 Enterprise SP1 上运行。

提前致谢!

【问题讨论】:

  • 问题解决了吗?

标签: c# wpf binding


【解决方案1】:

我终于弄明白了。绑定到一些写入不正确的数据,导致第二个 ItemsControl 无效。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-10
    • 2010-10-26
    • 2011-06-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多