Tag: contentcontrol

将ContentControl绑定到UserControl,并重用相同的实例

我正在尝试将ContentControl的内容绑定到我在ViewModel中实例化的UserControl。 我不能使用绑定到ViewModel的方法,然后让UserControl成为ViewModel的DataTemplate,因为我需要ContentControl的内容能够使用UserControls / Views的相同实例频繁更改,而不是实例化每次重新绑定时的视图。 但是,当将UserControl属性设置为UserControl实例时,然后当呈现视图/数据绑定时,我得到: 必须在连接到新的父Visual之前断开指定的子视图与当前父Visual。 即使我以前没有将此UserControl添加到任何地方,我只是更早地创建了这个实例并将其保存在内存中。 有没有更好的方法来实现我的目标? 在ViewModel中 public class MyViewModel : INotifyPropertyChanged { //… private void LoadApps() { var instances = new List { new Instance1View(), new Instance2View(), new Instance3View(), }; SwitchInstances(instances); } private void SwitchInstances(List instances) { CenterApp = instances[0]; } //… private UserControl _centerApp; public UserControl CenterApp { get { return […]

为动态内容绑定ContentControl内容

我目前正在尝试通过使用ListView(作为选项卡)和带有绑定内容属性的ContentControl来实现带有隐藏选项卡的tabcontrol的function。 我读了一下这个主题,如果我做对了,它应该这样工作: . . 并在代码背后: public partial class MainWindow : MetroWindow { private ContentControl SettingsPage; private ResourceDictionary SettingsPagesDict = new ResourceDictionary(); public MainWindow() { InitializeComponent(); SettingsPagesDict.Source = new Uri(“SettingsPages.xaml”, UriKind.RelativeOrAbsolute); SettingsPage = SettingsPagesDict[“AppearancePage”] as ContentControl; 尽管它没有抛出任何错误,但它不会显示“Test”TextBlock。 我可能有错误的绑定概念,请给我一个正确方向的提示。 问候

如何在代码中设置控制模板?

我在XAML中有这个 我想在C#代码中实现相同的function。 我怎样才能做到这一点? ControlTemplate ct = new ControlTemplate();.. Image img = new Image();.. 现在如何将此图像分配给控制模板? 我们能做到这一点还是我错过了这里的任何概念?