2010-11-05 141 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