2012-02-27 6 views
0

Я хотел что-то поместить в ViewBox, а затем этот ViewBox будет добавлен в шаблон ячейки DataGridTemplateColumn. Все это делалось в коде позади (C#).Вставить Viewbox в ячейку DataGrid

Я сделал это с Label, но я хотел бы использовать код ViewBox.The с помощью ярлыка является:

DataGridTemplateColumn dgtc5 = new DataGridTemplateColumn(); 

      dgtc5.Width = 142; 
      dgtc5.Header = "Page Life Expectancy"; 
      DataTemplate dtemp5 = new DataTemplate(); 
      FrameworkElementFactory fef5 = new FrameworkElementFactory(typeof(Label)); 

Binding b5 = new Binding("PleChart");   
fef5.SetBinding(Label.ContentProperty, b5); 

dtemp5.VisualTree = fef5; 
dgtc5.CellTemplate = dtemp5; 

ответ

2

Смотрите мой ответ здесь Binding a DataGrid's CellTemplate content to an Element or a DependencyProperty defined on the templated CustomControl.

Простое изменение шаблона данных, как этот

<DataTemplate x:Key="template2"> 
    <Viewbox> 
     <StackPanel Orientation="Horizontal"> 
      <TextBlock Text="Funky: "/> 
      <TextBlock Text="{Binding Path=Test, RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type local:CustomControl1}}}"/> 
     </StackPanel> 
    </Viewbox> 
</DataTemplate> 

вызывает содержащийся текст расти/сжиматься, чтобы соответствовать ViewBox.

Или просто сделать это:

<DataGrid.Columns> 
    <DataGridTemplateColumn Header="Test"> 
     <DataGridTemplateColumn.CellTemplate> 
      <DataTemplate> 
       <Viewbox> 
        <TextBlock Text="{Binding Name}"/> 
       </Viewbox> 
      </DataTemplate> 
     </DataGridTemplateColumn.CellTemplate> 
    </DataGridTemplateColumn> 
</DataGrid.Columns>