none
Tootip 如何设置动态的样式。 RRS feed

  • 问题

  • 我的DataGrid中有很多不同类型的数据,在数据库中存储的是一样的。是在Grid中现在的时候没有特别的要求,可是在tootlip 中显现有一定的要求。他要更加不同的类型选择不同的方式,小弟只知道如何设置样式,可是在设置之前它的数据需要转换一次,根据不同的类型显示不同的数据。  本人在这里绞尽脑汁未能想到一个很好的办法。请高人指点,我在绑定的时候使用IValueConerter 方式转换数据,但是不知道如何设置tootlip 的模板选择器,或者样式。请高手赐教。

    2012年4月17日 2:13

答案

  • 其实不用你这么麻烦,我们完全可以通过WPF提供的 DataTemplateSelector 来实现同台根据数据选择不同Template。 下面是一个简单的例子,用不同的模板来显示不同数据类型的。

          <DataGrid ItemsSource="{Binding Items}" CanUserAddRows="False" AutoGenerateColumns="False">
            <DataGrid.Resources>
              <DataTemplate x:Key="StringTemplate">
                <StackPanel>
                  <TextBlock Text="This is String Type Cell - "/>
                  <TextBlock Text="{Binding}"/>
                </StackPanel>
              </DataTemplate>
    
              <DataTemplate x:Key="IntTemplate">
                <StackPanel>
                  <TextBlock Text="This is Int Type Cell - "/>
                  <TextBlock Text="{Binding}"/>
                </StackPanel>
              </DataTemplate>
    
              <DataTemplate x:Key="DoubleTemplate">
                <StackPanel>
                  <TextBlock Text="This is Double Type Cell - "/>
                  <TextBlock Text="{Binding}"/>
                </StackPanel>
              </DataTemplate>
              
              <DataTemplate x:Key="CustomClassTemplate">
                <StackPanel>
                  <TextBlock Text="This is CustomClass Type Cell - "/>
                  <TextBlock Text="{Binding}"/>
                </StackPanel>
              </DataTemplate>
     
            </DataGrid.Resources>
            <DataGrid.Columns>
              <DataGridTextColumn Header="String" Binding="{Binding StringCell}" Width="*">
                <DataGridTextColumn.CellStyle>
                  <Style TargetType="{x:Type DataGridCell}">
                    <Setter Property="ToolTip">
                      <Setter.Value>
                        <ContentControl Content="{Binding StringCell}" ContentTemplateSelector="{StaticResource CellDataTemplateSelector}"/>
                      </Setter.Value>
                    </Setter>
                  </Style>
                </DataGridTextColumn.CellStyle>
              </DataGridTextColumn>
              <DataGridTextColumn Header="Int" Binding="{Binding IntCell}" Width="*">
                <DataGridTextColumn.CellStyle>
                  <Style TargetType="{x:Type DataGridCell}">
                    <Setter Property="ToolTip">
                      <Setter.Value>
                        <ContentControl Content="{Binding IntCell}" ContentTemplateSelector="{StaticResource CellDataTemplateSelector}"/>
                      </Setter.Value>
                    </Setter>
                  </Style>
                </DataGridTextColumn.CellStyle>
              </DataGridTextColumn>
              <DataGridTextColumn Header="Double" Binding="{Binding DoubleCell}" Width="*">
                <DataGridTextColumn.CellStyle>
                  <Style TargetType="{x:Type DataGridCell}">
                    <Setter Property="ToolTip">
                      <Setter.Value>
                        <ContentControl Content="{Binding DoubleCell}" ContentTemplateSelector="{StaticResource CellDataTemplateSelector}"/>
                      </Setter.Value>
                    </Setter>
                  </Style>
                </DataGridTextColumn.CellStyle>
              </DataGridTextColumn>
              <DataGridTextColumn Header="Custom Class" Binding="{Binding CustomClassCell}" Width="*">
                <DataGridTextColumn.CellStyle>
                  <Style TargetType="{x:Type DataGridCell}">
                    <Setter Property="ToolTip">
                      <Setter.Value>
                        <ContentControl Content="{Binding CustomClassCell}" ContentTemplateSelector="{StaticResource CellDataTemplateSelector}"/>
                      </Setter.Value>
                    </Setter>
                  </Style>
                </DataGridTextColumn.CellStyle>
              </DataGridTextColumn>
            </DataGrid.Columns>
          </DataGrid>

    C#:

      public partial class MainWindow : Window
      {
        public ObservableCollection<Item> Items { get; set; }
        public MainWindow()
        {
          InitializeComponent();
          Items = new ObservableCollection<Item>();
          for (int i = 0; i < 100; i++)
            Items.Add(new Item());
          this.DataContext = this;
        }
      }
      public class CellDataTemplateSelector : DataTemplateSelector
      {
        public override DataTemplate SelectTemplate(object item, DependencyObject container)
        {
          FrameworkElement element = container as FrameworkElement;
    
          if (element != null && item != null)
          {
            if (item is string)
              return element.FindResource("StringTemplate") as DataTemplate;
            else if (item is int)
              return element.FindResource("IntTemplate") as DataTemplate;
            else if (item is double)
              return element.FindResource("DoubleTemplate") as DataTemplate;
            else if (item is CustomClass)
              return element.FindResource("CustomClassTemplate") as DataTemplate;
          }
          return null;
        }
      }
    
      #region DataModel
      public class Item
      {
        static int ID;
        public string StringCell { get; set; }
        public int IntCell { get; set; }
        public double DoubleCell { get; set; }
        public CustomClass CustomClassCell { get; set; }
    
        public Item()
        {
          StringCell = "StringCell " + ID.ToString();
          IntCell = ID;
          DoubleCell = ID * 0.01;
          CustomClassCell = new CustomClass();
          ID++;
        }
      }
      public class CustomClass { }
      #endregion

    完整例子下载: https://skydrive.live.com/?cid=51b2fdd068799d15#cid=51B2FDD068799D15&id=51B2FDD068799D15%21951


    Bob Bao [MSFT]
    MSDN Community Support | Feedback to us

    2012年4月17日 6:08
    版主