2011-11-01 121 views
1

我有一個名爲MessageElementControl的用戶控件。它包含在MessageElementContainerControl中。當我打電話給MessageElementControl時,我試圖從容器傳遞一些屬性,但這些屬性未被設置。當我嘗試在MessageElementControl內使用它們時,它們爲空。有問題的屬性是ParentCollectionParentObject。我這樣做是爲了當用戶對MessageElementControl進行更新或刪除等操作時,我可以對集合進行必要的更改。這裏是容器的XAML。問題是如何讓這些屬性獲得正確的值?或者如果我應該採取完全不同的方法來處理整件事情?從itemscontrol到usercontrol的綁定

<UserControl x:Class="Bix.MessageElementContainerControl" Loaded="ThisControl_Loaded" 
      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" 
      mc:Ignorable="d" x:Name="ThisControl" xmlns:my="clr-namespace:Bix" 
      > 
    <UserControl.Resources> 

     <ObjectDataProvider x:Key="MessageElementDataProvider" ObjectType="{x:Type my:MessageElementDataProvider}"/> 

     <ObjectDataProvider x:Key="MessageUIElements" 
      ObjectInstance="{StaticResource MessageElementDataProvider}" 
      MethodName="GetUIMessageElements"/> 

     <DataTemplate x:Key="MessageElement"> 
      <my:MessageElementControl Width="{Binding ElementName=ThisControl,Path=Width}" 
              Element="{Binding Path=Element}" 
              ParentCollection="{Binding ElementName=ThisControl,Path=ItemsSource}" 
              ParentObject="{Binding ElementName=ThisControl,Path=ParentObjectSource}"/> 
     </DataTemplate> 

    </UserControl.Resources> 
    <StackPanel Height="Auto" Name="panel1"> 
     <StackPanel Orientation="Horizontal" Height="Auto" Name="panel2"> 
      <Label Content="Attachments and Data" FontSize="18" FontWeight="Bold" Foreground="#FF5A5A5A" Height="34" HorizontalAlignment="Left" Name="label6" VerticalAlignment="Top" Width="{Binding ElementName=ThisControl,Path=LabelWidth}" /> 
      <Button Margin="0,4" Height="25" HorizontalAlignment="Left" Name="btnNew" Padding="3" VerticalAlignment="Top" Width="60" Click="btnNew_Click"> 
       <TextBlock FontSize="12" FontWeight="Bold" Foreground="#FF3C3C3C" Text="New" TextWrapping="Wrap"/> 
      </Button> 
     </StackPanel> 
     <ScrollViewer ScrollViewer.VerticalScrollBarVisibility="Auto" ScrollViewer.HorizontalScrollBarVisibility="Auto" Width="{Binding ElementName=ThisControl,Path=Width}"> 
      <ItemsControl Name="itmsElements" Margin="0" 
          ItemsSource="{Binding Source={StaticResource MessageUIElements}}" 
          Width="{Binding ElementName=ThisControl,Path=Width}" 
          ItemTemplate="{Binding Source={StaticResource MessageElement}}"> 

      </ItemsControl> 
     </ScrollViewer> 
    </StackPanel> 
</UserControl> 

UPDATE:found it。我正在使用ThisControl作爲Container和包含的名稱。所以綁定並沒有真正發生。找到了一篇關於調試wpf綁定的有用文章,這些文章幫助我弄清楚了這一點。 devcomponents.com/blog/?p=312

+0

嘗試在您的視圖模型中將'ItemsControl.ItemSource'綁定到'ObservableCollection'。集合中的每個更改都會立即更新控件。 –

+0

這是一個可觀察的事情..這是另一個問題。我在各地使用observables,而itemscontrol似乎並不在乎我何時對源集合進行更改。我需要重置ItemsSource以使其刷新UI。但在這個問題上的問題是由相互衝突的用戶控件名稱引起的。我正在使用ThisControl作爲Container和包含的名稱。找到了一篇關於調試wpf綁定的有用文章,這些文章幫助我弄清楚了這一點。 http://devcomponents.com/blog/?p=312 – Orson

回答

0

我本來以爲要走的路將是你的收集容器您選擇的項目綁定到ObservableCollectionMessageUIElements)的MessageElement,到MessageElement類型的屬性都在實現在INotifyPropertyChanged接口的類您希望從代碼中驅動所選項目的情況。

+0

我很確定這是我有的架構..但由於某種原因,ItemsControl不響應它。 – Orson

相關問題