2010-11-05 145 views
1

我有2個用戶控制一個名爲過濾器和一個命名的FilterItemSilverlight中綁定組合框

過濾器看起來是這樣的:

<UserControl xmlns:my="clr-namespace:AttorneyDashboard.Views.UserControls" x:Class="AttorneyDashboard.Views.UserControls.Filters" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    xmlns:helpers="clr-namespace:AttorneyDashboard.Helpers" 
    mc:Ignorable="d" 
    d:DesignHeight="150" d:DesignWidth="590" x:Name="FiltersRoot"> 
    <Grid> 
     <ListBox x:Name="myListBox" ItemsSource="{Binding Path=FilterItems, ElementName=FiltersRoot}" > 
      <ListBox.ItemTemplate> 
       <DataTemplate> 
        <my:FilterItem ColumnsList="{Binding Path=Columns_, ElementName=FiltersRoot}" /> 
       </DataTemplate> 
      </ListBox.ItemTemplate> 
     </ListBox> 
    </Grid> 
</UserControl> 

代碼背後:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Net; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Documents; 
using System.Windows.Input; 
using System.Windows.Media; 
using System.Windows.Media.Animation; 
using System.Windows.Shapes; 
using System.Windows.Data; 
using System.Collections.ObjectModel; 
using System.Diagnostics; 
using AttorneyDashboard.Helpers; 

namespace AttorneyDashboard.Views.UserControls 
{ 
    public class MyItems 
    { 
     public string Header { get; set; } 
    } 
    public partial class Filters : UserControl 
    { 
     public Filters() 
     { 
      InitializeComponent(); 

     } 
     private DependencyProperty FilterItemsProperty = DependencyProperty.Register("FilterItems", typeof(ObservableCollection<FilterDescriptor>), typeof(Filters), new PropertyMetadata(null, new PropertyChangedCallback(OnChangeFilterItems))); 
     public ObservableCollection<FilterDescriptor> FilterItems 
     { 
      get 
      { 
       return (ObservableCollection<FilterDescriptor>)GetValue(FilterItemsProperty); 
      } 
      set 
      { 
       SetValue(FilterItemsProperty, value); 
      } 
     } 


     public static void OnChangeFilterItems(DependencyObject d, DependencyPropertyChangedEventArgs e) 
     { 
     } 



     public List<MyItems> Columns_ 
     { 
      get 
      { 
       List<MyItems> list = new List<MyItems>(); 
       list.Add(new MyItems() { Header = "test1" }); 
       list.Add(new MyItems() { Header = "test2" }); 
       list.Add(new MyItems() { Header = "test3" }); 
       return list; 
      } 
     } 
    } 
} 

FilterItems樣子此

<UserControl x:Class="AttorneyDashboard.Views.UserControls.FilterItem" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    mc:Ignorable="d" 
    d:DesignHeight="23" d:DesignWidth="590" xmlns:my="clr-namespace:AttorneyDashboard.Helpers" x:Name="FilterItemRoot"> 
    <StackPanel Orientation="Horizontal"> 
     <ComboBox Height="23" HorizontalAlignment="Left" Name="FieldName" VerticalAlignment="Top" Width="120" Margin="5,0,0,0" ItemsSource="{Binding Path=ColumnsList, ElementName=FilterItemRoot}" SelectedItem="{Binding PropertyPath, Mode=TwoWay}" DisplayMemberPath="Header"/> 
    </StackPanel> 
</UserControl> 

後面的代碼:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Net; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Documents; 
using System.Windows.Input; 
using System.Windows.Media; 
using System.Windows.Media.Animation; 
using System.Windows.Shapes; 
using System.Collections.ObjectModel; 
using AttorneyDashboard.Helpers; 
using System.Windows.Data; 

namespace AttorneyDashboard.Views.UserControls 
{ 
    public partial class FilterItem : UserControl 
    { 
     public FilterItem() 
     { 
      InitializeComponent(); 
     } 

     private DependencyProperty ColumnsListProperty = DependencyProperty.Register("ColumnsList", typeof(List<MyItems>), typeof(FilterItem), new PropertyMetadata(null, new PropertyChangedCallback(OnChangeColumns))); 
     public List<MyItems> ColumnsList 
     { 
      get 
      { 
       return (List<MyItems>)GetValue(ColumnsListProperty); 
      } 
      set 
      { 
       SetValue(ColumnsListProperty, value); 
      } 
     } 

     public static void OnChangeColumns(DependencyObject d, DependencyPropertyChangedEventArgs e) 
     { 
     } 
    } 
} 

FilterItems的數量是確定的(這意味着FilterItems結合工程確定),但人口只有最後的FilterItem的組合框... 我不知道究竟是錯的...

更新: 我發現爲什麼,但我stll不知道解決辦法... 它接縫,他的特性是之前的FilterItem的內容綁定.. 所以的FilterItem的組合框綁定在Columns屬性綁定之前...

+0

您是不是想在綁定表達式中使用「PropertyPath」:SelectedItem =「{Binding PropertyPath,Mode = TwoWay}」? – 2010-11-05 15:39:58

+0

..yeap ..我想我還需要一個轉換器..但這不是綁定不起作用的原因.... – bogdanbrudiu 2010-11-05 16:12:59

回答

0

你的代碼中的FilterItem

private DependencyProperty ColumnsListProperty = DependencyProperty 
    .Register("ColumnsList", typeof(List<MyItems>), typeof(FilterItem), 
     new PropertyMetadata(null, new PropertyChangedCallback(OnChangeColumns))); 

請,使之靜:

private **static** DependencyProperty ColumnsListProperty = DependencyProperty 
    .Register("ColumnsList", typeof(List<MyItems>), typeof(FilterItem), 
     new PropertyMetadata(null, new PropertyChangedCallback(OnChangeColumns))); 

完蛋了。

P.S. :在過濾器使依賴項屬性也是靜態的,一般在任何地方都可以。:)

-1

您已將x:Name屬性直接放置在您的UserControl元素上。不要這樣做。使用這種模式,而不是: -

<UserControl x:Class="AttorneyDashboard.Views.UserControls.FilterItem" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    mc:Ignorable="d" 
    d:DesignHeight="23" d:DesignWidth="590" xmlns:my="clr-namespace:AttorneyDashboard.Helpers" > 
    <StackPanel Orientation="Horizontal" x:Name="LayoutRoot"> 
     <ComboBox Height="23" HorizontalAlignment="Left" Name="FieldName" VerticalAlignment="Top" Width="120" Margin="5,0,0,0" ItemsSource="{Binding Path=Parent.ColumnsList, ElementName=LayoutRoot}" SelectedItem="{Binding PropertyPath, Mode=TwoWay}" DisplayMemberPath="Header"/> 
    </StackPanel> 
</UserControl> 

你是不是在分配給用戶控件的名稱的控制,即在XAML的範圍所屬的使用你的用戶控件。如果您的代碼在內部要求包含UserControl具有特定名稱,那麼事情可能會中斷。

+0

綁定路徑= ColumnsList,ElementName = LayoutRoot.Parent根本不起作用...而不是綁定路徑= Parent.ColumnsList,ElementName = LayoutRoot有同樣的問題......只有最後一個實例有組合框填充 – bogdanbrudiu 2010-11-07 14:39:32

+0

@bogdanbrudiu:你對我來說有點腦衰。 – AnthonyWJones 2010-11-07 19:54:18