Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ListBox中Usercontrol的Bind DependencyProperty_C#_Wpf_Binding_User Controls_Datacontext - Fatal编程技术网

C# ListBox中Usercontrol的Bind DependencyProperty

C# ListBox中Usercontrol的Bind DependencyProperty,c#,wpf,binding,user-controls,datacontext,C#,Wpf,Binding,User Controls,Datacontext,我需要列出用户控件的列表框。我的用户控件有文本框。所以我想在UserControl的文本框中显示列表子项的属性。我已经尝试了很多关于DataContext和ElementName的选项,但都不起作用。我只是踩了一下。使其工作的唯一方法是删除UserControl的DataContext绑定并更改项属性名称,使其与DependencyProperty名称匹配-但我需要在具有不同实体的不同ViewModel中重用控件,因此几乎不可能使用该方法 有趣的是,若我将UserControl更改为Textbo

我需要列出用户控件的列表框。我的用户控件有文本框。所以我想在UserControl的文本框中显示列表子项的属性。我已经尝试了很多关于DataContext和ElementName的选项,但都不起作用。我只是踩了一下。使其工作的唯一方法是删除UserControl的DataContext绑定并更改项属性名称,使其与DependencyProperty名称匹配-但我需要在具有不同实体的不同ViewModel中重用控件,因此几乎不可能使用该方法

有趣的是,若我将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>
窗口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>

窗口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; }
        }
    }
公共部分类主窗口:窗口
{
公共主窗口()
{
_列表=新的ObservableCollection();
_添加(新项目(“Sam”);
_列表。添加(新项目(“应用”);
_增加(新项目(“H**”);
初始化组件();
}
私有可观察收集列表;
公共可观测集合MyList
{
获取{return\u list;}
集合{}
}
}
公共类项目
{
公共项(字符串名称)
{
_名称=名称;
}
私有字符串\u名称;
公共字符串名
{
获取{return\u name;}
设置{u name=value;}
}
}

这在XAML中是一个相当大的难题。问题是,在用户控件中执行此操作时:

DataContext="{Binding RelativeSource={RelativeSource Self}}"
您可以更改其数据上下文,以便在此行中:

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

“这在XAML中是一个相当大的难题。”-这是一个相当保守的说法,我花了很多时间才最终找到答案
<local:MyControl Text="{Binding Path=Name}"/>
<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>