2016-11-30 47 views
0

我的目标是将格式字符串(如“A__B”)传递到DataGrid列的标题,并将其显示为带下标B的 A(即A_B )。为了在每列的基础上做到这一点,我打算为此使用模板如下。WPF DataGridColumnHeader模板中的简单数学样式的数据绑定/转换

<DataGridTextColumn Binding="{Binding AB}", Header="A__B" HeaderStyle="{StaticResource ColumnHeaderTemplate}" />    

为了完成合适的模板的实现,我想我可能想使用转换器。因此,我写了一个简单的转换器,它将字符串拆分为Symbol类的对象,并具有属性Text和Subscript。

using System; 
using System.Windows.Data; 
using System.Globalization; 
using System.ComponentModel; 
using System.Runtime.CompilerServices; 

namespace Test 
{ 
    public class Symbol : INotifyPropertyChanged 
    { 
     string text_, subscript_; 

     public event PropertyChangedEventHandler PropertyChanged; 

     private void NotifyPropertyChanged([CallerMemberName] String propertyName = "") 
     { 
      PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
     } 

     public Symbol(string text, string subscript) 
     { 
      Text = text; 
      Subscript = subscript; 

      NotifyPropertyChanged(); 
     } 

     public String Text 
     { 
      get { return text_; } 
      set { text_ = value; NotifyPropertyChanged(); } 
     } 

     public String Subscript 
     { 
      get { return subscript_; } 
      set { subscript_ = value; NotifyPropertyChanged(); } 
     } 
    } 

    [ValueConversion(typeof(string), typeof(Symbol))] 
    public class StringToSymbolConverter : IValueConverter 
    { 
     public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 
     { 
      if (parameter == null) return null; 

      var format = parameter as string; 

      int idx = format.IndexOf("__"); 
      if (idx < 0) return new Symbol(format, ""); 

      return new Symbol(format.Substring(0, idx), format.Substring(idx + 2); 
     } 

     public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) 
     { 
      return null; 
     } 
    } 
} 

我加入此转换器窗口资源

<Window.Resources> 
    <l:StringToSymbolConverter x:Key="stringToSymbolConverter" /> 
</Window.Resources> 

而在我的数据网格我做了以下

<DataGrid x:Name="dataGrid" ItemsSource="{Binding Results}" AutoGenerateColumns="False"> 

     <DataGrid.Resources> 
      <Style TargetType="{x:Type DataGridColumnHeader}" x:Key="ColumnHeaderTemplate"> 
       <Setter Property="ContentTemplate"> 
        <Setter.Value> 
         <DataTemplate> 
          <TextBlock TextWrapping="Wrap" DataContext="{Binding Converter={StaticResource stringToSymbolConverter}}"> 
           <Run Text="{Binding Path=Text}"/> 
           <Run Text="{Binding Path=Subscript}" BaselineAlignment="Subscript" FontSize="8"/> 
          </TextBlock> 
         </DataTemplate> 
        </Setter.Value> 
       </Setter> 
      </Style> 
     </DataGrid.Resources> 

     <DataGrid.Columns> 
      <DataGridTextColumn Binding="{Binding AB, Mode=OneWay}" ClipboardContentBinding="{x:Null}" Header="A__B" HeaderStyle="{StaticResource ColumnHeaderTemplate}" />   
     </DataGrid.Columns> 
    </DataGrid> 

我在这里的想法是做转换 再一次将驻留在Symbol对象中的组件插入正确的位置。为此,我试图(错误地)使用文本块的DataContext。

现在,这不能按预期工作,我得不到可见的输出。我看到该模板正在应用到相应的列,所以我似乎有这个权利。另外,如果我用纯文本替换绑定或添加回退值,则文本将正确呈现。我怀疑绑定失败并在运行时传递一个空字符串。作为WPF的新手,并且在XAML/WPF中提供了相当有限的调试工具链,这对我来说很难找出哪里出了问题以及从哪里开始。

我可能已经在绑定游戏中获得了所涉及的依赖关系,完全错误,这可能是由于我缺乏对底层机制的理解,尤其是涉及到模板时。因此,我很感激任何有用的提示!

任何想法?

+2

你是什么意思_“没有按预期工作”_?请澄清您获得的结果以及它们与预期的不同之处。 – Grx70

+0

好的,我明白你的意思了。我的意思是我没有看到预期的结果(在头文件中呈现A__B),我不知道底下会发生什么,因为我不知道如何调试这样的东西。如果我删除绑定并用纯文本替换它们,文本就会显示出来,所以很明显,我在这里绑定数据的想法肯定是错误的。我会尽我所能来纠正这个问题。谢谢! –

+0

我们已经建立了您没有看到的内容,但是您看到**是什么**?没有? ' “A__B”'? '“AB”'没有下标?还有别的吗? – Grx70

回答

1

错误在于您的转换器 - 即您试图转换该参数,而不是该值,因为您没有指定ConverterParameter作为使用转换器的绑定的参数,因此该参数为null。因此你得到一个空的列标题。应该转换,而不是值:

public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 
{ 
    //here you should use 'value' instead of 'parameter' 
    if (value == null) return null; 
    var format = value as string; 

    int idx = format.IndexOf("__"); 
    if (idx < 0) return new Symbol(format, ""); 

    return new Symbol(format.Substring(0, idx), format.Substring(idx + 2); 
} 

注意,这种错误是很容易调试 - 当你面对其中转换涉及绑定问题是有益的设置断点在Convert方法穿过它。

+0

我的确放置了一个断点,但不幸的是在空检查之后,出于某种原因,我认为转换从未触发过。我的错...但我真的很高兴,因为我错过了一些关于绑定机制如何工作的重要东西,我完全疯了。非常感谢你的帮助 :-)! –