Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.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# 自定义控件绑定问题(Windows universal 10)_C#_Wpf_Mvvm_Binding_Win Universal App - Fatal编程技术网

C# 自定义控件绑定问题(Windows universal 10)

C# 自定义控件绑定问题(Windows universal 10),c#,wpf,mvvm,binding,win-universal-app,C#,Wpf,Mvvm,Binding,Win Universal App,我正在尝试将自定义控件的参数绑定到列表。但是,它在错误的ViewModel中搜索。它在控件的ViewModel(ViewModelUserControlVM)中搜索,而不是在控件所在页面的ViewModel中搜索 用户控制xaml <UserControl.DataContext> <vm:ViewModelUserControlVM/> </UserControl.DataContext> <ListView Name="lst">

我正在尝试将自定义控件的参数绑定到列表。但是,它在错误的ViewModel中搜索。它在控件的ViewModel(ViewModelUserControlVM)中搜索,而不是在控件所在页面的ViewModel中搜索

用户控制xaml

 <UserControl.DataContext>
    <vm:ViewModelUserControlVM/>
</UserControl.DataContext>

<ListView Name="lst">
    <ListView.ItemsPanel>
        <ItemsPanelTemplate>
            <StackPanel Orientation="Horizontal"/>
        </ItemsPanelTemplate>
    </ListView.ItemsPanel>
</ListView>
主页xaml

    <local:CustomControl ItemsSource="{Binding list, Mode=TwoWay}">

提前谢谢。

也许它会这样工作:

 <local:CustomControl ItemsSource="{x:Bind ViewModel.list, Mode=TwoWay}">


如果UserControl的DataContext是ViewModelUserControlVM,则其中的任何绑定都将指向该DataContext,您可以找到有关x:Bind的其他信息。如果需要父控件绑定到dependency属性以从其(父控件)DataContext传递内容,则可以将UserControls主面板(网格、StackPanel等)的DataContext设置为ViewModelUserControlVM。这将导致控件本身在可视树中“向上”查找以找到DataContext。在本例中,将显示主页的viewmodel

您共享的部分代码显示,您试图将ItemsSource依赖项属性绑定到主页中的某个内容,但随后通过将整个UserControl的DataContext分配给不同的viewmodel来覆盖UserControl的DataContext

更完整的代码将支持或质疑这一理论

以下更新

谢谢您提供的附加代码。现在还不清楚最终目标是什么-例如,提到了选定的项目,但没有使用,我们不知道用户控件的viewmodel的相关性。因此,我创建了一个简单的东西,它在主视图模型中显示了一个列表,并绑定到一个正在查找该类型列表的usercontrol。我希望它能帮助你到达你想要去的地方。。。 (注意:我使用了MVVMLight库)

主页虚拟机:

public class MainPageVM : ViewModelBase
    {
        private List<Model> _list = new List<Model>();
        public List<Model> list
        {
            get { return _list; }
            set { Set(ref _list, value); }
        }



        public MainPageVM()
        {
            for (int i = 0; i < 5; i++)
            {
                list.Add(new Model("url" + i, "title" + i, "desc" + i));
            }
            RaisePropertyChanged(() => list);
        }
    }
public类MainPageVM:ViewModelBase
{
私有列表_List=新列表();
公开名单
{
获取{return\u list;}
集合{set(ref _列表,值);}
}
公共MainPageVM()
{
对于(int i=0;i<5;i++)
{
添加(新型号(“url”+i,“标题”+i,“说明”+i));
}
RaisePropertyChanged(()=>list);
}
}
MainPage xaml(DataContext是MainVM):


CustomControl代码隐藏(请注意,ctor-datacontext控件中的datacontext设置是使用它的内容,但LayoutRoot网格中的控件将使用控件的依赖属性):

publiccustomcontrol()
{
this.InitializeComponent();
LayoutRoot.DataContext=this;
}
公共列表项资源
{
获取{return(List)GetValue(ItemsSourceProperty);}
set{SetValue(ItemsSourceProperty,value);}
}
//使用DependencyProperty作为ItemsSource的备份存储。这将启用动画、样式、绑定等。。。
公共静态只读依赖项Property ItemsSourceProperty=
DependencyProperty.Register(“ItemsSource”、typeof(List)、typeof(CustomControl)、new PropertyMetadata(null、new PropertyChangedCallback(OnItemSourceChanged));
私有静态资源已更改(DependencyObject d、DependencyPropertyChangedEventArgs e)
{
}
}
CustomControl xaml:

<UserControl
    x:Class="App8.CustomControl"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:App8"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="300"
    d:DesignWidth="400">

    <Grid x:Name="LayoutRoot">
        <ListView Name="lst" ItemsSource="{Binding ItemsSource, Mode=OneWay}" 
                  DisplayMemberPath="Url">
        </ListView>
    </Grid>
</UserControl>


简而言之,ListView的itemsource绑定到CustomControl的ItemsSource依赖项属性。CustomControl的ItemsSource的DataContext是使用该控件的任何DataContext。我希望它能帮助您。

我不喜欢使用x:bind,因为它不使用ViewModel。它很接近,但现在我的CustomControl中的绑定不起作用。这是一个绑定在控件中的样式,我应用于Listview。我正在尝试解决这个问题。如果我这样做的话。Style templatedControl=((CustomControl)sender).lst.Style;SetterBaseCollection set=templatedControl.Setters;List=set.ToList();Setter Setter=(Setter)列表[13];然而,我无法在那个setter中获得控件。这是一个控件模板。
        public CustomControl()
        {
        this.InitializeComponent();
        }
 <local:CustomControl ItemsSource="{x:Bind ViewModel.list, Mode=TwoWay}">
public class MainPageVM : ViewModelBase
    {
        private List<Model> _list = new List<Model>();
        public List<Model> list
        {
            get { return _list; }
            set { Set(ref _list, value); }
        }



        public MainPageVM()
        {
            for (int i = 0; i < 5; i++)
            {
                list.Add(new Model("url" + i, "title" + i, "desc" + i));
            }
            RaisePropertyChanged(() => list);
        }
    }
<Page
    x:Class="App8.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:App8"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">
    <Page.DataContext>
        <local:MainPageVM />
    </Page.DataContext>

    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <local:CustomControl ItemsSource="{Binding list, Mode=OneWay}" />
    </Grid>
</Page>
public CustomControl()
        {
            this.InitializeComponent();
            LayoutRoot.DataContext = this;
        }



        public List<Model> ItemsSource
        {
            get { return (List<Model>)GetValue(ItemsSourceProperty); }
            set { SetValue(ItemsSourceProperty, value); }
        }

        // Using a DependencyProperty as the backing store for ItemsSource.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ItemsSourceProperty =
            DependencyProperty.Register("ItemsSource", typeof(List<Model>), typeof(CustomControl), new PropertyMetadata(null, new PropertyChangedCallback(OnItemsSourceChanged)));

        private static void OnItemsSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {

        }
    }
<UserControl
    x:Class="App8.CustomControl"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:App8"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="300"
    d:DesignWidth="400">

    <Grid x:Name="LayoutRoot">
        <ListView Name="lst" ItemsSource="{Binding ItemsSource, Mode=OneWay}" 
                  DisplayMemberPath="Url">
        </ListView>
    </Grid>
</UserControl>