绑定DataGridTextColumn标头不起作用

时间:2019-04-28 08:46:09

标签: c# wpf data-binding

我正在尝试将文本绑定到其他语言的窗口。 但是绑定到数据网格标题不起作用 (不起作用意味着什么也不显示,就像一个空字符串)...

我有很多代码,所以我提供了重要的部分...告诉我是否错过任何信息

此绑定对我的按钮文字起作用

<Button  MinWidth="75"  MinHeight="75" MaxHeight="100" Margin="10" FontSize="25" Content="{Binding PreTestInformationProjectAccessNames.PreTestInformationProjectAccessHighText}"></Button>

我还尝试使用这种方式绑定到标头,但没有成功

  <DataGridTextColumn x:Name="Teur_ProjectColumn" Binding="{Binding Teur_Project}">
                            <DataGridTextColumn.Header>
                                <TextBlock Text="{Binding PreTestInformationProjectAccessNames.PreTestInformationProjectAccessHighText}"/>
                            </DataGridTextColumn.Header>
                        </DataGridTextColumn>

Windows:

<Window x:Class="PulserTesterMultipleHeads.UserControls.PreTestInformationProjectAccess"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
            xmlns:local="clr-namespace:PulserTesterMultipleHeads.UserControls"
            mc:Ignorable="d"
            Title="PreTestInformationProjectAccess" Height="616.5" Width="800">
        <Grid>

     <StackPanel>
<DataGrid IsReadOnly="True" Name="DataGridProject" ItemsSource="{Binding ProjectList}" AutoGenerateColumns="False"
                        SelectedItem="{Binding SelectedRowItem,Mode=TwoWay}"  
                        DockPanel.Dock="Bottom" MaxHeight="300" MinHeight="300">
                        <DataGrid.Columns>
                            <DataGridTextColumn x:Name="Teur_ProjectColumn" Binding="{Binding Teur_Project}"
                                                Header="{Binding PreTestInformationProjectAccessNames.PreTestInformationProjectAccessHighText}"/>


<Button  MinWidth="75"  MinHeight="75" MaxHeight="100" Margin="10" FontSize="25" Content="{Binding PreTestInformationProjectAccessNames.PreTestInformationProjectAccessHighText}"></Button>


    </StackPanel>
        </Grid>
    </Window>

我后面的代码:

using PulserTesterMultipleHeads.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace PulserTesterMultipleHeads.UserControls
{
    /// <summary>
    /// Interaction logic for PreTestInformationProjectAccess.xaml
    /// </summary>
    public partial class PreTestInformationProjectAccess : Window
    {
        public PreTestInformationProjectAccess()
        {
            InitializeComponent();
            DataContext = new PreTestInformationProjectAccessVM();
            if (((PreTestInformationProjectAccessVM)DataContext).CloseAction == null)
                ((PreTestInformationProjectAccessVM)DataContext).CloseAction = new Action(this.Close);
        }
    }
}

我在ModelView中的相关部分(如果错过任何相关信息,请告诉我)

using PulserTesterMultipleHeads.UserControls;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ExtentionMethods;
using static DataBaseManager.LocalPulserDBManager;
using MilBatDBModels.PulserTester;
using System.IO.Ports;
using System.Configuration;
using MilBatDBModels;
using PulserTesterMultipleHeads.Classes;
using PulserTesterMultipleHeads.Classes.NamesLanguage;

namespace PulserTesterMultipleHeads.Models
{
    public class PreTestInformationProjectAccessVM : INotifyPropertyChanged
    {
        public PreTestInformationProjectAccessNames PreTestInformationProjectAccessNames { get; set; }

     public PreTestInformationProjectAccessVM()
       {
        PreTestInformationProjectAccessNames = new 
        PreTestInformationProjectAccessNames();
        PreTestInformationProjectAccessNames.SetNames();


       }
        public event PropertyChangedEventHandler PropertyChanged;
    }
}

我的PreTestInformationProjectAccessNames类:

using MilBatDBModels.PulserTester;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace PulserTesterMultipleHeads.Classes.NamesLanguage
{
    public class PreTestInformationProjectAccessNames : INotifyPropertyChanged
    {
        public string PreTestInformationProjectAccessCcvText { get; set; }
        public string PreTestInformationProjectAccessDeltaText { get; set; }
        public string PreTestInformationProjectAccessCurrentText { get; set; }
        public string PreTestInformationProjectAccessHighText { get; set; }
        public string PreTestInformationProjectAccessLowText { get; set; }
        public string PreTestInformationProjectAccessRangeText { get; set; }
        public string PreTestInformationProjectAccessTimeText { get; set; }
        public string PreTestInformationProjectAccessOcvText { get; set; }
        public string PreTestInformationProjectAccessProjectDescriptionColumnText { get; set; }
        public string PreTestInformationProjectAccessCodeProjectColumnText { get; set; }
        public string PreTestInformationProjectAccessCodeAgeCellColumnText { get; set; }
        public string PreTestInformationProjectAccessCurrentColumnText { get; set; }
        public string PreTestInformationProjectAccessTimeColumnText { get; set; }
        public string PreTestInformationProjectAccessCellsInParallelColumnText { get; set; }
        public string PreTestInformationProjectAccessCellsInSeriesColumnText { get; set; }
        public string PreTestInformationProjectAccessCurrentQCColumnText { get; set; }
        public string PreTestInformationProjectAccessTimeQCColumnText { get; set; }
        public string PreTestInformationProjectAccessdADOcvQCColumnText { get; set; }
        public string PreTestInformationProjectAccessdAdCcvQCColumnText { get; set; }
        //public string PreTestInformationProjectAccessDepartmentColumnText { get; set; }

        public FlowDirection FlowDirectionLanguage { get; set; }
        public FlowDirection FlowDirectionLanguageText { get; set; }
        public HorizontalAlignment HorizontalAlignmentControls { get; set; }

        public event PropertyChangedEventHandler PropertyChanged;

        public void SetNames()
        {
            List<LanguageItem> it = Globals.LanguageItems;
            LanguageType lt = Globals.LanguageType;
            FlowDirectionLanguage = FlowDirection.RightToLeft;
            FlowDirectionLanguageText = FlowDirection.LeftToRight;
            HorizontalAlignmentControls = HorizontalAlignment.Left;
            if (lt.Equals(LanguageType.Hebrew))
            {
                FlowDirectionLanguage = FlowDirection.LeftToRight;
                FlowDirectionLanguageText = FlowDirection.RightToLeft;
                HorizontalAlignmentControls = HorizontalAlignment.Right;
                PreTestInformationProjectAccessCcvText = it.Where(l => l.ID.Equals("PreTestInformationProjectAccessCcvText")).First().Hebrew;
                PreTestInformationProjectAccessDeltaText = it.Where(l => l.ID.Equals("PreTestInformationProjectAccessDeltaText")).First().Hebrew;
                PreTestInformationProjectAccessCurrentText = it.Where(l => l.ID.Equals("PreTestInformationProjectAccessCurrentText")).First().Hebrew;
                PreTestInformationProjectAccessHighText = it.Where(l => l.ID.Equals("PreTestInformationProjectAccessHighText")).First().Hebrew;
                PreTestInformationProjectAccessLowText = it.Where(l => l.ID.Equals("PreTestInformationProjectAccessLowText")).First().Hebrew;
                PreTestInformationProjectAccessRangeText = it.Where(l => l.ID.Equals("PreTestInformationProjectAccessRangeText")).First().Hebrew;
                PreTestInformationProjectAccessTimeText = it.Where(l => l.ID.Equals("PreTestInformationProjectAccessTimeText")).First().Hebrew;
                PreTestInformationProjectAccessOcvText = it.Where(l => l.ID.Equals("PreTestInformationProjectAccessOcvText")).First().Hebrew;
                PreTestInformationProjectAccessProjectDescriptionColumnText = it.Where(l => l.ID.Equals("PreTestInformationProjectAccessProjectDescriptionColumnText")).First().Hebrew;
                PreTestInformationProjectAccessCodeProjectColumnText = it.Where(l => l.ID.Equals("PreTestInformationProjectAccessCodeProjectColumnText")).First().Hebrew;
                PreTestInformationProjectAccessCodeAgeCellColumnText = it.Where(l => l.ID.Equals("PreTestInformationProjectAccessCodeAgeCellColumnText")).First().Hebrew;
                PreTestInformationProjectAccessCurrentColumnText = it.Where(l => l.ID.Equals("PreTestInformationProjectAccessCurrentColumnText")).First().Hebrew;
                PreTestInformationProjectAccessTimeColumnText = it.Where(l => l.ID.Equals("PreTestInformationProjectAccessTimeColumnText")).First().Hebrew;
                PreTestInformationProjectAccessCellsInParallelColumnText = it.Where(l => l.ID.Equals("PreTestInformationProjectAccessCellsInParallelColumnText")).First().Hebrew;
                PreTestInformationProjectAccessCellsInSeriesColumnText = it.Where(l => l.ID.Equals("PreTestInformationProjectAccessCellsInSeriesColumnText")).First().Hebrew;
                PreTestInformationProjectAccessCurrentQCColumnText = it.Where(l => l.ID.Equals("PreTestInformationProjectAccessCurrentQCColumnText")).First().Hebrew;
                PreTestInformationProjectAccessTimeQCColumnText = it.Where(l => l.ID.Equals("PreTestInformationProjectAccessTimeQCColumnText")).First().Hebrew;
                PreTestInformationProjectAccessdADOcvQCColumnText = it.Where(l => l.ID.Equals("PreTestInformationProjectAccessdADOcvQCColumnText")).First().Hebrew;
                PreTestInformationProjectAccessdAdCcvQCColumnText = it.Where(l => l.ID.Equals("PreTestInformationProjectAccessdAdCcvQCColumnText")).First().Hebrew;
                //PreTestInformationProjectAccessDepartmentColumnText = it.Where(l => l.ID.Equals("PreTestInformationProjectAccessDepartmentColumnText")).First().Hebrew;

            }
            if (lt.Equals(LanguageType.Germany))
            {
                PreTestInformationProjectAccessCcvText = it.Where(l => l.ID.Equals("PreTestInformationProjectAccessCcvText")).First().Germany;
                PreTestInformationProjectAccessDeltaText = it.Where(l => l.ID.Equals("PreTestInformationProjectAccessDeltaText")).First().Germany;
                PreTestInformationProjectAccessCurrentText = it.Where(l => l.ID.Equals("PreTestInformationProjectAccessCurrentText")).First().Germany;
                PreTestInformationProjectAccessHighText = it.Where(l => l.ID.Equals("PreTestInformationProjectAccessHighText")).First().Germany;
                PreTestInformationProjectAccessLowText = it.Where(l => l.ID.Equals("PreTestInformationProjectAccessLowText")).First().Germany;
                PreTestInformationProjectAccessRangeText = it.Where(l => l.ID.Equals("PreTestInformationProjectAccessRangeText")).First().Germany;
                PreTestInformationProjectAccessTimeText = it.Where(l => l.ID.Equals("PreTestInformationProjectAccessTimeText")).First().Germany;
                PreTestInformationProjectAccessOcvText = it.Where(l => l.ID.Equals("PreTestInformationProjectAccessOcvText")).First().Germany;
                PreTestInformationProjectAccessProjectDescriptionColumnText = it.Where(l => l.ID.Equals("PreTestInformationProjectAccessProjectDescriptionColumnText")).First().Germany;
                PreTestInformationProjectAccessCodeProjectColumnText = it.Where(l => l.ID.Equals("PreTestInformationProjectAccessCodeProjectColumnText")).First().Germany;
                PreTestInformationProjectAccessCodeAgeCellColumnText = it.Where(l => l.ID.Equals("PreTestInformationProjectAccessCodeAgeCellColumnText")).First().Germany;
                PreTestInformationProjectAccessCurrentColumnText = it.Where(l => l.ID.Equals("PreTestInformationProjectAccessCurrentColumnText")).First().Germany;
                PreTestInformationProjectAccessTimeColumnText = it.Where(l => l.ID.Equals("PreTestInformationProjectAccessTimeColumnText")).First().Germany;
                PreTestInformationProjectAccessCellsInParallelColumnText = it.Where(l => l.ID.Equals("PreTestInformationProjectAccessCellsInParallelColumnText")).First().Germany;
                PreTestInformationProjectAccessCellsInSeriesColumnText = it.Where(l => l.ID.Equals("PreTestInformationProjectAccessCellsInSeriesColumnText")).First().Germany;
                PreTestInformationProjectAccessCurrentQCColumnText = it.Where(l => l.ID.Equals("PreTestInformationProjectAccessCurrentQCColumnText")).First().Germany;
                PreTestInformationProjectAccessTimeQCColumnText = it.Where(l => l.ID.Equals("PreTestInformationProjectAccessTimeQCColumnText")).First().Germany;
                PreTestInformationProjectAccessdADOcvQCColumnText = it.Where(l => l.ID.Equals("PreTestInformationProjectAccessdADOcvQCColumnText")).First().Germany;
                PreTestInformationProjectAccessdAdCcvQCColumnText = it.Where(l => l.ID.Equals("PreTestInformationProjectAccessdAdCcvQCColumnText")).First().Germany;
                //PreTestInformationProjectAccessDepartmentColumnText = it.Where(l => l.ID.Equals("PreTestInformationProjectAccessDepartmentColumnText")).First().Germany;
            }
            if (lt.Equals(LanguageType.English))
            {
                PreTestInformationProjectAccessCcvText = it.Where(l => l.ID.Equals("PreTestInformationProjectAccessCcvText")).First().English;
                PreTestInformationProjectAccessDeltaText = it.Where(l => l.ID.Equals("PreTestInformationProjectAccessDeltaText")).First().English;
                PreTestInformationProjectAccessCurrentText = it.Where(l => l.ID.Equals("PreTestInformationProjectAccessCurrentText")).First().English;
                PreTestInformationProjectAccessHighText = it.Where(l => l.ID.Equals("PreTestInformationProjectAccessHighText")).First().English;
                PreTestInformationProjectAccessLowText = it.Where(l => l.ID.Equals("PreTestInformationProjectAccessLowText")).First().English;
                PreTestInformationProjectAccessRangeText = it.Where(l => l.ID.Equals("PreTestInformationProjectAccessRangeText")).First().English;
                PreTestInformationProjectAccessTimeText = it.Where(l => l.ID.Equals("PreTestInformationProjectAccessTimeText")).First().English;
                PreTestInformationProjectAccessOcvText = it.Where(l => l.ID.Equals("PreTestInformationProjectAccessOcvText")).First().English;
                PreTestInformationProjectAccessProjectDescriptionColumnText = it.Where(l => l.ID.Equals("PreTestInformationProjectAccessProjectDescriptionColumnText")).First().English;
                PreTestInformationProjectAccessCodeProjectColumnText = it.Where(l => l.ID.Equals("PreTestInformationProjectAccessCodeProjectColumnText")).First().English;
                PreTestInformationProjectAccessCodeAgeCellColumnText = it.Where(l => l.ID.Equals("PreTestInformationProjectAccessCodeAgeCellColumnText")).First().English;
                PreTestInformationProjectAccessCurrentColumnText = it.Where(l => l.ID.Equals("PreTestInformationProjectAccessCurrentColumnText")).First().English;
                PreTestInformationProjectAccessTimeColumnText = it.Where(l => l.ID.Equals("PreTestInformationProjectAccessTimeColumnText")).First().English;
                PreTestInformationProjectAccessCellsInParallelColumnText = it.Where(l => l.ID.Equals("PreTestInformationProjectAccessCellsInParallelColumnText")).First().English;
                PreTestInformationProjectAccessCellsInSeriesColumnText = it.Where(l => l.ID.Equals("PreTestInformationProjectAccessCellsInSeriesColumnText")).First().English;
                PreTestInformationProjectAccessCurrentQCColumnText = it.Where(l => l.ID.Equals("PreTestInformationProjectAccessCurrentQCColumnText")).First().English;
                PreTestInformationProjectAccessTimeQCColumnText = it.Where(l => l.ID.Equals("PreTestInformationProjectAccessTimeQCColumnText")).First().English;
                PreTestInformationProjectAccessdADOcvQCColumnText = it.Where(l => l.ID.Equals("PreTestInformationProjectAccessdADOcvQCColumnText")).First().English;
                PreTestInformationProjectAccessdAdCcvQCColumnText = it.Where(l => l.ID.Equals("PreTestInformationProjectAccessdAdCcvQCColumnText")).First().English;
                //PreTestInformationProjectAccessDepartmentColumnText = it.Where(l => l.ID.Equals("PreTestInformationProjectAccessDepartmentColumnText")).First().English;
            }
        }

    }
}

1 个答案:

答案 0 :(得分:1)

视图模型不是标头DataContext中的默认TextBlock,但是您可以使用{RelativeSource}绑定到视图模型:

<DataGridTextColumn x:Name="Teur_ProjectColumn" Binding="{Binding Teur_Project}">
    <DataGridTextColumn.Header>
        <TextBlock Text="{Binding DataContext.PreTestInformationProjectAccessNames.PreTestInformationProjectAccessHighText,
                            RelativeSource={RelativeSource AncestorType=Window}}"/>
    </DataGridTextColumn.Header>
</DataGridTextColumn>