我有2个用户控件,一个名为Filters,另一个名为FilterItem

过滤器如下所示:

<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绑定(bind)可以正常运行),但是仅填充最后一个FilterItem的组合框...
而且我不知道到底是哪里错了...

更新:
我找到了原因,但是我不知道解决方案...
它表明FilterItem的内容在其属性之前已绑定(bind)。
因此,在绑定(bind)Columns属性之前,已绑定(bind)FilterItem中的组合框。

最佳答案

您在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)));

而已。

附言:在过滤器中也使依赖项属性静态化,通常在任何地方都可以:)

关于silverlight - 嵌套控件中的Silverlight绑定(bind)组合框,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/4107375/

10-12 22:30