Wpf 如何绑定到datacontext之外的内容
我在WPF中有一个位于布局根目录中的列表框 我在布局根目录中也有一个框架 listbox由具有stringName和framework elementUI的项组成 如何将框架的内容绑定为listbox的selected item属性的UI属性Wpf 如何绑定到datacontext之外的内容,wpf,data-binding,mvvm,Wpf,Data Binding,Mvvm,我在WPF中有一个位于布局根目录中的列表框 我在布局根目录中也有一个框架 listbox由具有stringName和framework elementUI的项组成 如何将框架的内容绑定为listbox的selected item属性的UI属性 如果您需要代码隐藏,您将如何在MVVM中执行此操作听起来好像您想要显示所选对象的对象列表和详细信息。如果我是对的,MVVM中的解决方案可能如下所示: <ListView ItemsSource="{Binding ObjectsList}" IsSy
如果您需要代码隐藏,您将如何在MVVM中执行此操作听起来好像您想要显示所选对象的对象列表和详细信息。如果我是对的,MVVM中的解决方案可能如下所示:
<ListView ItemsSource="{Binding ObjectsList}" IsSynchronizedWithCurrentItem="True" />
<ContentControl Content="{Binding ObjectsList}">
<ContentControl.ContentTemplate>
<DataTemplate>
<!-- details template -->
</DataTemplate>
</ContentControl.ContentTemplate>
</ContentControl>
我使用ContentControl而不是Frame,因为绑定到Content属性时遇到问题,所以在绑定更改后,我从未让它刷新。我没有执行正确的MVVM,数据不应该托管在视图中 XAML: 代码隐藏:
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
namespace BindDemo
{
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
Data = new List<DataItem>();
Data.Add(new DataItem("TextBox", new TextBox(){ Text="hello" }));
Data.Add(new DataItem("ComboBox", new ComboBox()));
Data.Add(new DataItem("Slider", new Slider()));
DataContext = Data;
}
public List<DataItem> Data
{
get; private set;
}
}
public class DataItem
{
public DataItem(string name, FrameworkElement ui)
{
Name = name;
UI = ui;
}
public string Name { get; private set; }
public FrameworkElement UI { get; private set; }
}
}
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
namespace BindDemo
{
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
Data = new List<DataItem>();
Data.Add(new DataItem("TextBox", new TextBox(){ Text="hello" }));
Data.Add(new DataItem("ComboBox", new ComboBox()));
Data.Add(new DataItem("Slider", new Slider()));
DataContext = Data;
}
public List<DataItem> Data
{
get; private set;
}
}
public class DataItem
{
public DataItem(string name, FrameworkElement ui)
{
Name = name;
UI = ui;
}
public string Name { get; private set; }
public FrameworkElement UI { get; private set; }
}
}