简体   繁体   English

为什么绑定对WPF DataGrid上的DataGrildTemplateColumn不起作用?

[英]Why is binding not working for a DataGrildTemplateColumn on a WPF DataGrid?

I have a DataGrid defined in a WPF UserControl (MVVM). 我在WPF用户控件(MVVM)中定义了一个DataGrid I am attempting to use DataGridTemplateColumns to define DataGrid content. 我正在尝试使用DataGridTemplateColumns定义DataGrid内容。

Here is what my the data looks like in my ViewModel: 这是我的数据在ViewModel中的样子:

public class CompanyClass
{
   public string Name { get; set;}
   public string Location { get; set; }
}

public ObservableCollection<CompanyClass> CompanyList = // Fill with Data;

Here is what the DataGrid definition looks like in XAML: 这是XAML中的DataGrid定义:

<Grid Name="SampleGrid"
      Width="{Binding ActualWidth, ElementName=NegativeAccountsBalances}" 
      HorizontalAlignment="Stretch" 
      VerticalAlignment="Stretch">
  <DataGrid Name="SampleDataGrid"
            AutoGenerateColumns="False"
            ItemsSource="{Binding CompanyList}"
            Width="{Binding Path=ActualWidth, RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=Grid,AncestorLevel=1}}"
            IsReadOnly="True"
            SelectionMode="Extended"
            SelectionUnit="CellOrRowHeader"
            HorizontalScrollBarVisibility="Auto"  
            VerticalScrollBarVisibility="Auto">
    <DataGrid.Columns>
      <DataGridTemplateColumn Width="SizeToCells" IsReadOnly="True">
        <DataGridTemplateColumn.CellTemplate>
          <DataTemplate>
            <StackPanel Orientation="Vertical">
              <TextBlock Name="txtCompanyName" Text="{Binding Path=Name}" Height="200" Width="200"/>
              <TextBlock Text="{Binding Location}" Height="20" Width="20"/>
            </StackPanel>
          </DataTemplate>
        </DataGridTemplateColumn.CellTemplate>
      </DataGridTemplateColumn>
    </DataGrid.Columns>
  </DataGrid>
</Grid>

For some reason I can't seem to get the binding working for the TextBlock controls within my DataTemplate . 由于某种原因,我似乎无法使DataTemplateTextBlock控件的绑定正常工作。 I'm setting the ItemSource to the collection of "Company" contained in my ViewModel. 我将ItemSource设置为ViewModel中包含的“ Company”的集合。

What am I missing/doing wrong here? 我在这里想念/做错了什么?

I don't see any errors in the VS output window. 我在VS输出窗口中没有看到任何错误。 If I try to bind a TextBlock to a bogus value, I still don't see any errors. 如果我尝试将TextBlock绑定到伪造的值,我仍然看不到任何错误。 Somehow I don't seem to have my binding set up correctly. 似乎我似乎没有正确设置绑定。

Can someone please point me the right direction? 有人可以给我指出正确的方向吗?

Thanks 谢谢

CompanyList is a field. CompanyList是一个字段。 You can't bind to fields. 您无法绑定到字段。 You can only bind to properties. 您只能绑定到属性。 It needs a getter and a setter. 它需要一个getter和setter。

Make sure that you initialize the CompanyList property only once , either in the constructor or by using the initializer syntax that was introduced in C#6: 确保仅在构造函数中或使用C#6中引入的初始化程序语法对CompanyList属性进行一次初始化:

public class ViewModel
{
    public ViewModel()
    {
        CompanyList = new ObservableCollection<CompanyClass>();
        //popuoate collection...
        CompanyList.Add(new CompanyClass() { Name = "test...." });
    }
    public ObservableCollection<CompanyClass> CompanyList { get; private set; }
}

C#6+: C#6 +:

public ObservableCollection<CompanyClass> CompanyList { get; } = new ObservableCollection<CompanyClass>();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM