1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

[Silverlight] DataGridのHeaderにバインドしたい。

Last updated at Posted at 2016-01-20

DataGridのHeaderにBindすると"System.Windows.Data.Binding"が表示されて困ったので対処したときのメモ。

バインドするデータ

MainWindowViewModel.cs
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace SilverlightApplication4
{
    public class MainWindowViewModel
    {
        public string Header1
        {
            get {return "Test1" ; }
        }

        public string Header2
        {
            get {return "Test2" ; }
        }

        public string Header3
        {
            get {return "Test3" ; }
        }
    }
}

DataGridを持つ画面。右列が困った例。
左列がその対策

MainPage.xaml
<UserControl x:Class="SilverlightApplication4.MainPage"
    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:my="clr-namespace:SilverlightApplication4"
    xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data"
xmlns:dataprimitives="clr-namespace:System.Windows.Controls.Primitives;assembly=System.Windows.Controls.Data"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400">
    <UserControl.DataContext>
        <my:MainWindowViewModel x:Name="vm"></my:MainWindowViewModel>
    </UserControl.DataContext>

    <Grid x:Name="LayoutRoot" Background="White">
        <data:DataGrid>
            <data:DataGrid.Columns>
                <data:DataGridTextColumn>
                    <data:DataGridTextColumn.HeaderStyle>
                        <Style TargetType="dataprimitives:DataGridColumnHeader">
                            <Setter Property="Template">
                                <Setter.Value>
                                    <ControlTemplate>
                                        <TextBlock Text="{Binding Header1}" VerticalAlignment="Center" />
                                    </ControlTemplate>
                                </Setter.Value>
                            </Setter>
                        </Style>
                    </data:DataGridTextColumn.HeaderStyle>
                </data:DataGridTextColumn>
                <data:DataGridTextColumn Header="Test2"></data:DataGridTextColumn>
                <data:DataGridTextColumn Header="Test3"></data:DataGridTextColumn>
                <data:DataGridTextColumn Header="{Binding Header1}"></data:DataGridTextColumn>
            </data:DataGrid.Columns>
        </data:DataGrid>
    </Grid>
</UserControl>
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?