【问题标题】:Bind tab to observablecollection in model in xaml将选项卡绑定到 xaml 模型中的 observablecollection
【发布时间】:2012-05-14 21:26:14
【问题描述】:

我有一个关于标签数据绑定的问题。 我有以下 xaml 代码:

<Window x:Class="SuperAtomsController.GUI.windowAnalog"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"          
    Title="windowAnalog" Height="900" Width="1400"
    DataContext="{Binding RelativeSource={RelativeSource self}}">

    <Grid>
        <TabControl Height="459" HorizontalAlignment="Left" Margin="188,278,0,0" Name="tabControl1" ItemsSource="{Binding Path=model.sequences}" VerticalAlignment="Top" Width="883">

        </TabControl>
    </Grid>
</Window>

以及背后的代码:

public partial class windowAnalog : Window
{
    public Data model;                           
    public windowAnalog(Data model)
    {
        this.model = model;
        InitializeComponent();                             
    }
}

但是,tabcontrol 中没有出现任何内容(model.sequences 的类型为ObservableCollection&lt;&gt;)。如果删除 xaml 中的 itemssource 并在 InitializeComponent(); 之后的 c# 代码 tabControl1.ItemsSource = model.sequences; 中添加以下内容,则可以正常工作。我错过了什么?

【问题讨论】:

    标签: xaml c#-4.0 data-binding observablecollection


    【解决方案1】:

    显然您的绑定没有解决,请检查您的调试输出窗口以获取有用的诊断消息。

    我不记得了,但我认为模型可能需要成为一个属性而不是一个字段才能使 WPF 属性路径起作用。否则可能是 DataContext 的问题。您可以尝试在 InitializeComponent() 而不是 DataContext xaml 之前对您的构造函数执行 this.DataContext = this。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-09
      • 2016-08-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多