【问题标题】:Why does Binding within a CellTemplate not work? [closed]为什么 CellTemplate 中的绑定不起作用? [关闭]
【发布时间】:2020-06-17 07:56:39
【问题描述】:

我正在尝试将名为“MaterialList”的 ObservableCollection 绑定到放置在列中的 Combobox。 C#

public class Delivery
{
//Some other Properties which work
...
public ObservableCollection<string> MaterialList { get; set; }
public ObservableCollection<OpenPosition> OpenPositions { get; set; }

public Delivery()
{
  this.MaterialList = new ObservableCollection<string>();
  this.OpenPositions = new ObservableCollection<OpenPosition>();
}
}

我检查了该列表中是否有数据,并且有! XAML:

                    <dxg:GridControl x:Name="grid4" MaxHeight="2000" AutoGenerateColumns="None" ItemsSource="{Binding SelectedDelivery.OpenPositions}">     
                            <dxg:GridColumn" Header="{Binding LanguageTexts[_MaterialNumber]}">
                                <dxg:GridColumn.CellTemplate>
                                    <DataTemplate>
                                        <ComboBox Name="ComboBox" ItemsSource="{Binding SelectedDelivery.MaterialList}" SelectedIndex="0" />
                                    </DataTemplate>
                                </dxg:GridColumn.CellTemplate>
                            </dxg:GridColumn>

我的 ComboBox 不显示任何内容。我猜有什么问题,因为我使用了 Itemssource 中的 DataContext。 希望你能帮助我!

【问题讨论】:

    标签: wpf xaml binding


    【解决方案1】:

    尝试将绑定设置为ItemsSource="{Binding DataContext.SelectedDelivery.MaterialList, RelativeSource={RelativeSource FindAncestor, AncestoryType={x:Type ItemsControl}}"

    问题就像你想的那样,组合框的数据上下文来自SelectedDelivery.OpenPositions,我建议的方式是使用视图的 DataContext,而不是来自 ItemsSource。

    希望对你有帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-15
      • 2018-02-15
      • 2017-12-13
      • 1970-01-01
      相关资源
      最近更新 更多