在ListBox中绑定Usercontrol的DependencyProperty

时间:2014-05-05 15:55:34

标签: c# wpf binding user-controls datacontext

我需要ListBox,其中列出了我的UserControl。我的UserControl有TextBox。所以我想在UserControl的textBox中显示List的子项的属性。我已经尝试了很多使用DataContext和ElementName的选项 - 它只是不起作用。我只是坚持下去。使其工作的唯一方法是删除UserControl的DataContext绑定到自身并更改Item属性名称,使其与DependencyProperty名称匹配 - 但我需要在具有不同实体的不同视图模型中重用我的控件,因此几乎不可能使用方法

有趣的是,如果我将UserControl更改为Textbox并绑定它的Text属性 - 一切正常。 Textbox和我的UserControl有什么区别?

所以让我展示我的代码。 我已将代码简化为仅显示必要:

控制XAML:

<UserControl x:Class="TestControl.MyControl"
         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" 
         d:DesignHeight="100" d:DesignWidth="200"
         DataContext="{Binding RelativeSource={RelativeSource Self}}">
    <Grid>
        <TextBlock Text="{Binding Text}"/>
    </Grid>
</UserControl>

控制CS:

    public partial class MyControl : UserControl
    {
        public MyControl()
        {
            InitializeComponent();
        } 
        public string Text
        {
            get { 
                return (string)this.GetValue(TextProperty); }
            set { 
                this.SetValue(TextProperty, value); }
        }
        public static DependencyProperty TextProperty = DependencyProperty.Register("Text", typeof(string), typeof(MyControl), new propertyMetadata(""));
}

Window XAML:

<Window x:Class="TestControl.MainWindow"
        Name="_windows"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:TestControl"
        Title="MainWindow" Height="350" Width="525" >
    <Grid Name="RootGrid">
        <ListBox ItemsSource="{Binding ElementName=_windows, Path=MyList}">
            <ItemsControl.ItemTemplate >
                <DataTemplate >
                    <local:MyControl Text="{Binding Path=Name}"/>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ListBox>
    </Grid>
</Window>

Window CS:

public partial class MainWindow : Window
    {
        public MainWindow()
        {
            _list = new ObservableCollection<Item>();
            _list.Add(new Item("Sam"));
            _list.Add(new Item("App"));
            _list.Add(new Item("H**"));
            InitializeComponent();

        }
        private ObservableCollection<Item> _list;

        public ObservableCollection<Item> MyList
        {
            get { return _list;}
            set {}
        }
    }
    public class Item
    {
        public Item(string name)
        {
            _name = name;
        }

        private string _name;

        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }
    }

1 个答案:

答案 0 :(得分:0)

这是XAML中的一个非常重要的问题。问题是,当您在用户控件中执行此操作时:

DataContext="{Binding RelativeSource={RelativeSource Self}}"

您可以更改其数据上下文,以便在此行中:

<local:MyControl Text="{Binding Path=Name}"/>

运行时现在将尝试解析&#34; Name&#34;在&#34; MyControl&#34;的实例上,而不是在继承的数据上下文(即视图模型)上。 (通过检查“输出”窗口确认 - 您应该看到该效果的绑定错误。)

您可以使用RelativeSource绑定来解决此问题,而不是以这种方式设置用户控件的数据上下文:

<UserControl x:Class="TestControl.MyControl"
         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" 
         d:DesignHeight="100" d:DesignWidth="200"
    <Grid>
        <TextBlock 
            Text="{Binding Text,RelativeSource={RelativeSource AncestorType=UserControl}}"
        />
    </Grid>
</UserControl>
相关问题