【问题标题】:silverlight binding combobox in nested controls嵌套控件中的 silverlight 绑定组合框
【发布时间】:2011-05-05 16:13:40
【问题描述】:

我有 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 绑定工作正常),但只有最后一个 FilterItem 的 Combobox 被填充... 而且我不知道到底是什么问题......

更新: 我找到了原因,但我仍然不知道解决方案...... 它似乎在他的属性之前绑定了 FilterItem 的内容。 所以FilterItem中的combobox是在绑定Columns属性之前绑定的...

【问题讨论】:

  • 您的意思是在绑定表达式中包含“PropertyPath”:SelectedItem="{Binding PropertyPath, Mode=TwoWay}"?
  • ..yeap.. 我想我也需要一个转换器.. 但这不是绑定不起作用的原因....

标签: silverlight data-binding mvvm


【解决方案1】:

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

就是这样。

附: : 在过滤器中,依赖属性也是静态的,通常在任何地方做:)

【讨论】:

    【解决方案2】:

    您已将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> 
    

    您无法控制分配给用户控件的名称,该名称属于使用您的 UserControl 的 Xaml 范围。如果您的代码在内部要求包含的 UserControl 具有特定名称,那么事情可能会中断。

    【讨论】:

    • Binding Path=ColumnsList, ElementName=LayoutRoot.Parent 根本不起作用...相反 Binding Path=Parent.ColumnsList, ElementName=LayoutRoot 有同样的问题...只有最后一个实例有组合框填充
    猜你喜欢
    • 2011-05-04
    • 2011-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多