【问题标题】:ItemsControl with ItemSource set to ObservableCollection of UserControls fails to render in visual tree将 ItemSource 设置为 UserControls 的 ObservableCollection 的 ItemsControl 无法在可视化树中呈现
【发布时间】:2010-09-18 22:42:52
【问题描述】:

TestEntryView.xaml.cs 内部

public partial class TestEntryView : UserControl
{
    public ObservableCollection<TestFieldView> Fields {get;set;}
    ...
}

TestFieldView 是一个 UserControl。

<UserControl x:Class="STS2Editor.View.TestEntryView"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:vw="clr-namespace:STS2Editor.View"
         mc:Ignorable="d" 
         x:Name="testEntryView" 
         d:DesignHeight="300" 
         d:DesignWidth="427"
         d:DataContext="{Binding TestEntry, Source={StaticResource Sample}}">
<Grid Background="{DynamicResource ButtonNormalBorder}" TextElement.Foreground="{DynamicResource TextBrush}">
    <Border Background="{DynamicResource ControlBackgroundBrush}" BorderBrush="{DynamicResource ControlBackgroundBrush}" BorderThickness="4" CornerRadius="16">

        <Grid Margin="4" >
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>
            <ScrollViewer Background="{DynamicResource ControlBackgroundBrush}" Grid.IsSharedSizeScope="True" Grid.Row="1">
                <ItemsControl x:Name="fieldList" ItemsSource="{Binding Fields, ElementName=testEntryView}"/>
            </ScrollViewer>
        </Grid>
    </Border>
</Grid>

绑定是正确的,但是当我窥探视觉树时,我的子项都由边框和内容呈现器组成,没有子视觉。

【问题讨论】:

    标签: c# wpf xaml user-controls


    【解决方案1】:

    如果由于某种原因未在您的 UserControl (TestFieldView) 上调用 LoadComponent 并且未正确解析 Xaml,我只能重新创建您的情况。这发生在 InitializeComponent 中(例如参见 What does InitializeComponent() do, and how does it work in WPF?)。你确定你在TestFieldView的constructor中调用它吗?

    【讨论】:

    • 原来是这样的!有一个重载调用失败,我现在觉得自己像个傻瓜!
    【解决方案2】:

    您确定这是连接到正确数据的绑定吗?我更喜欢

    {Binding Fields,RelativeSource={RelativeSource TemplatedParent}}
    

    来自用户控件的绑定方法,但它应该以任何一种方式工作(只要您确定它的绑定无关紧要(可能取决于您创建控件实例的方式)

    您似乎没有与您的 TestFieldView 项目或任何东西相关联的样式或 DataTemplate? 另外,由于您使用的是 ItemsControl,因此您可能还需要定义 ItemsPanel 模板。

    <ItemsControl Grid.Row="1" ItemsSource="{Binding Fields}">
       <ItemsControl.ItemsPanel>
         <ItemsPanelTemplate>
            <UniformGrid Rows="1" />
         </ItemsPanelTemplate>
       </ItemsControl.ItemsPanel>
    <ItemsControl.ItemTemplate>
        <DataTemplate>
           <CheckBox Content="{Binding  Name}" IsChecked="{Binding  IsSelected}"/>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
    

    如果不掌握您的代码并弄乱它直到它工作,则不确定,但它可能与项目控件、面板模板、数据模板方面有关。

    如果我完全误解了你可以尝试用 ListView 替换 ItemsControl(因为这至少应该向你显示一个类名列表),这会给你某种显示然后你可以从那里开始工作。

    【讨论】:

    【解决方案3】:

    “TestEntryView.Fields”属性是 DependencyProperty 吗?或者它至少会引发一个 PropertyChanged 事件?如果是这样,上面的例子并不清楚。如果它不是 DP 或没有引发 PropertyChanged 事件,请尝试其中一个。这可能是解决方案。

    编辑:

    要将其更改为依赖属性,请执行以下操作:

    public ObservableCollection<TestFieldView> Fields
    {
        get { return (ObservableCollection<TestFieldView>)GetValue(FieldsProperty); }
        set { SetValue(FieldsProperty, value); }
    }
    
    public static readonly DependencyProperty FieldsProperty =
        DependencyProperty.Register("Fields", typeof(ObservableCollection<TestFieldView>), typeof(MainWindow),
        new UIPropertyMetadata(new ObservableCollection<TestFieldView>()));
    

    【讨论】:

    • public ObservableCollection Fields {get;set;}
    • 是的,我看到了。我在问它是 DependencyProperty,还是实现了 INotifyPropertyChanged。
    • 如果不是,我是说您应该尝试将其设为依赖属性,或使其引发 PropertyChanged 通知。
    • Firoso,我知道。但我想也许你只是在缩写上面的代码(它发生在 StackOverflow 上),这就是我验证的原因。但无论如何,我确实提出了一个建议。如果它对你有用,那就太好了。如果没有,对不起。 (很容易投反对票,还有感叹号。我只是想帮忙)。干杯!
    • 无论哪种方式,DP 都不会导致此问题,因为它是一个可观察的集合,并且它自身的集合引用不会改变。我非常感谢您的帮助
    猜你喜欢
    • 2015-04-28
    • 2012-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-13
    • 1970-01-01
    • 1970-01-01
    • 2011-04-21
    相关资源
    最近更新 更多