将CustomControl绑定到DataGrid的Datatemplate内的ObservableCollection的项目时出错

时间:2018-11-05 10:56:29

标签: c# wpf data-binding wpf-controls wpfdatagrid

我有一个DataGrid,它具有一个站点的ObservableCollection作为源。我有一些绑定到Site属性的DataTemplate中定义的列。当使用诸如TextBlock之类的内置控件时,绑定可以完美工作,但我无法使用自定义控件来完成绑定。我已经看到该错误出现在控制台中,但我不完全了解我该怎么做:

  

System.Windows.Data错误:40:BindingExpression路径错误:在“对象”“ TestControl”(名称=“)”上找不到“端口”属性。 BindingExpression:Path =端口; DataItem ='TestControl'(Name ='');目标元素是'TestControl'(Name ='');目标属性为“ CameraName”(类型为“字符串”)

在这里附上我使用的代码片段;

DataGrid:

<DataGrid Name="SitesList" CanUserReorderColumns="True"  
                  ItemsSource="{Binding ViewModel.Sites}"
                  PreparingCellForEdit="DataGrid_PreparingCellForEdit" 
                  >                      
<DataGrid.Resources>
    <DataTemplate x:Key="CameraTemplate">
        <Grid>
            <hv:TestControl CameraName="{Binding Path=Port}"/>
        </Grid>
    </DataTemplate>
    <DataTemplate x:Key="PortTemplate">
        <TextBox x:Name="PortTextBox"
            Text="{Binding Path=Port, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
    </DataTemplate>
</DataGrid.Resources>
<DataGrid.Columns>
    <DataGridTemplateColumn Header="Camera"
                            CellTemplate="{StaticResource CameraTemplate}"
                            MinWidth="100"/>
    <DataGridTemplateColumn Header="Port"
                            CellTemplate="{StaticResource PortTemplate}"
                            MinWidth="70"/>
</DataGrid.Columns></DataGrid>

ObservableCollection:

    private ObservableCollection<Site> m_sites = new ObservableCollection<Site>();
    public ObservableCollection<Site> Sites
    {
        get
        {
            return m_sites;
        }
        set
        {
            m_sites = value;
        }
    }

Site.cs:

namespace Wizard.View
{
    public class Site: INotifyPropertyChanged
    {
        #region properties
        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(String info)
        {
            if (PropertyChanged != null) {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }
        private string m_port = "0";
        public string Port
        {
            get
            {
                return m_port;
            }
            set
            {
                m_port = value;
                NotifyPropertyChanged("Port");
            }
        }
        #endregion        
    }
}

TestControl.xaml.cs:

namespace Wizard.View.HelpersViews
{
    public partial class TestControl: UserControl
    {
        public TestControl()
        {
            InitializeComponent();
        }

        public string CameraName
        {
            get { return (string)GetValue(CameraNameProperty); }
            set {
                Console.WriteLine(value);
                SetValue(CameraNameProperty, value);
                CameraNameTextBlock.Text = value;
            }
        }

        public static readonly DependencyProperty CameraNameProperty =
            DependencyProperty.Register("CameraName",
                                        typeof(string),
                                        typeof(TestControl),
                                        new PropertyMetadata("0"));
    }
}

TestControl.xaml:

<UserControl
             x:Class="Wizard.View.HelpersViews.TestControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             DataContext="{Binding RelativeSource={RelativeSource Self}}"
             d:DesignHeight="100" d:DesignWidth="300">
    <StackPanel Orientation="Horizontal" Height="Auto" HorizontalAlignment="Left">
        <CheckBox Margin ="5,0" Name="AddCameraCheck"
                  VerticalAlignment="Center" Style="{StaticResource ConfiguratorCheckBox}" />
        <TextBlock x:Name="CameraNameTextBlock" Width="175" Text="{Binding CameraName}" />
    </StackPanel>
</UserControl>

“端口”列已正确绑定,但“相机”未正确绑定。

1 个答案:

答案 0 :(得分:1)

TextBlock中的TestControl应该绑定到自身的CameraName属性:

<TextBlock x:Name="CameraNameTextBlock" Width="175" Text="{Binding CameraName, 
                                       RelativeSource={RelativeSource AncestorType=UserControl}}" />

...但是您应该删除此控件,以便控件从DataContext继承DataGridRow

DataContext="{Binding RelativeSource={RelativeSource Self}}"

此外,依赖项属性的CLR包装器应仅调用GetValueSetValue

public string CameraName
{
    get { return (string)GetValue(CameraNameProperty); }
    set { SetValue(CameraNameProperty, value); }
}