2010-09-18 12 views
3

渲染內TestEntryView.xaml.cs的ItemsControl用的ItemSource設置爲用戶控件的的ObservableCollection未能在視覺樹

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

其中TestFieldView是一個用戶控件。

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

結合是正確的,但是當我窺探視覺樹我的子項都包含一個邊框和內容展示,沒有孩子的視覺效果。

回答

2

如果由於某種原因LoadComponent未在您的UserControl(TestFieldView)上調用並且Xaml未正確解析,我只能重新創建您的情況。這發生在InitializeComponent中(例如參見What does InitializeComponent() do, and how does it work in WPF?)。你確定你在TestFieldView的構造函數中調用它嗎?

+0

這實際上是這樣的!有一次超負荷沒有打電話給我,我現在覺得自己很傻! – Firoso 2010-09-22 22:42:32

0

「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>())); 
+0

public ObservableCollection Fields {get; set;} < - 明確列出張貼! – Firoso 2010-09-22 20:30:59

+1

是的,我看到了。我問是否它是一個DependencyProperty,或者它是否實現INotifyPropertyChanged。 – ASanch 2010-09-22 20:33:32

+1

如果不是,我說你應該嘗試使它成爲一個依賴項屬性,或者讓它提高PropertyChanged通知。 – ASanch 2010-09-22 20:34:26

0

你確定這是綁定了連接到正確的數據?我更喜歡

{Binding Fields,RelativeSource={RelativeSource TemplatedParent}} 

結合的方法,從用戶的控制範圍內,但它應該工作無論哪種方式(只要你相信它結合它不應該的問題(也許取決於你如何創建的實例你控制)

你似乎並不有一個風格或DataTemplate中與您的TestFieldView物品或東西嗎? 相關的加,因爲你用的是ItemsControl的,你可能需要定義ItemsPaneltemplate爲好。

<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(因爲這應該至少顯示一個類名稱列表),它會給你一些顯示,然後你可以從那裏工作。

+0

哦,也許對你有用http://www.galasoft.ch/mydotnet/articles/article-2007041201.aspx – PJUK 2010-09-21 16:38:23

相關問題