I've bind the static property in DataGridRow (not DataGridTextcolumn). How can I bind it?
I already have bind a static property in a normal grid manually. Code is shown below(But now how to bind a property in DataGridRow-Wise).
<Window x:Class="Data_Grid_Row_HeaderBindingTest.GridTest"
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:Data_Grid_Row_HeaderBindingTest"
mc:Ignorable="d"
Title="GridTest" Height="450" Width="800">
<Grid>
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<TextBlock Text="ArgumentName" Grid.Row="0" Grid.Column="0" FontSize="25" TextAlignment="Center" Margin="10"/>
<TextBlock Text="ArgumentValue" Grid.Row="0" Grid.Column="1" FontSize="25" TextAlignment="Center" Margin="10"/>
<TextBlock Text="IA" Grid.Row="1" Grid.Column="0" FontSize="25" TextAlignment="Center" Margin="10"/>
<TextBlock Text="IB" Grid.Row="2" Grid.Column="0" FontSize="25" TextAlignment="Center" Margin="10"/>
<TextBlock Text="IC" Grid.Row="3" Grid.Column="0" FontSize="25" TextAlignment="Center" Margin="10"/>
<TextBlock Text="ID" Grid.Row="4" Grid.Column="0" FontSize="25" TextAlignment="Center" Margin="10"/>
<TextBlock Text="IE" Grid.Row="5" Grid.Column="0" FontSize="25" TextAlignment="Center" Margin="10"/>
<TextBlock Text="IF" Grid.Row="6" Grid.Column="0" FontSize="25" TextAlignment="Center" Margin="10"/>
<TextBlock Text="IG" Grid.Row="7" Grid.Column="0" FontSize="25" TextAlignment="Center" Margin="10"/>
<TextBox Text="{Binding Path=(local:Model.IA),Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Grid.Row="1" Grid.Column="1" Width="360" Height="40"/>
<TextBox Text="{Binding Path=(local:Model.IB),Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Grid.Row="2" Grid.Column="1" Width="360" Height="40"/>
<TextBox Text="{Binding Path=(local:Model.IC),Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Grid.Row="3" Grid.Column="1" Width="360" Height="40"/>
<TextBox Text="{Binding Path=(local:Model.ID),Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Grid.Row="4" Grid.Column="1" Width="360" Height="40"/>
<TextBox Text="{Binding Path=(local:Model.IE),Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Grid.Row="5" Grid.Column="1" Width="360" Height="40"/>
<TextBox Text="{Binding Path=(local:Model.IF),Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Grid.Row="6" Grid.Column="1" Width="360" Height="40"/>
<TextBox Text="{Binding Path=(local:Model.IG),Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Grid.Row="7" Grid.Column="1" Width="360" Height="40"/>
<!--<DataGrid x:Name="MyGrid" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Header="Argument Name" Binding="{Binding ArgumentName}"></DataGridTextColumn>
<DataGridTextColumn Header="Argument Value" Binding="{Binding Path=(local:Model.IA),Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
></DataGridTextColumn>
</DataGrid.Columns>
--><!--<DataGrid.RowHeaderTemplate>
<DataTemplate>
<TextBlock Text="{Binding RelativeSource={RelativeSource Mode=FindAncestor,
AncestorType={x:Type DataGridRow}},
Path=Item.Row.Header}"/>
</DataTemplate>
</DataGrid.RowHeaderTemplate>--><!--
<DataGrid.RowHeaderStyle>
<Style TargetType="DataGridRowHeader">
<Setter Property="Content" Value="{Binding Path=(local:Model.Name)}"/>
</Style>
</DataGrid.RowHeaderStyle>
</DataGrid>-->
</Grid>
</Window>
Model Class
using System;
using System.Collections.Generic;
using System.Text;
namespace Data_Grid_Row_HeaderBindingTest
{
public class Model
{
private static int iA;
private static int iB;
private static int iC;
private static int iD;
private static int iE;
private static int iF;
private static int iG;
private static string name;
public static int IA
{
get
{
return iA;
}
set
{
iA = value;
// Raise a change event
OnFilterStringChanged(EventArgs.Empty);
}
}
public static int IB
{
get
{
return iB;
}
set
{
iB = value;
// Raise a change event
OnFilterStringChanged(EventArgs.Empty);
}
}
public static int IC
{
get
{
return iC;
}
set
{
iC = value;
// Raise a change event
OnFilterStringChanged(EventArgs.Empty);
}
}
public static int ID
{
get
{
return iD;
}
set
{
iD = value;
// Raise a change event
OnFilterStringChanged(EventArgs.Empty);
}
}
public static int IE
{
get
{
return iE;
}
set
{
iE = value;
// Raise a change event
OnFilterStringChanged(EventArgs.Empty);
}
}
public static int IF
{
get
{
return iF;
}
set
{
iF = value;
// Raise a change event
OnFilterStringChanged(EventArgs.Empty);
}
}
public static int IG
{
get
{
return iG;
}
set
{
iG = value;
// Raise a change event
OnFilterStringChanged(EventArgs.Empty);
}
}
public static string Name
{
get
{
return name;
}
set
{
name = value;
// Raise a change event
OnFilterStringChanged(EventArgs.Empty);
}
}
// Declare a static event representing changes to your static property
public static event EventHandler FilterStringChanged;
// Raise the change event through this static method
protected static void OnFilterStringChanged(EventArgs e)
{
EventHandler handler = FilterStringChanged;
if (handler != null)
{
handler(null, e);
}
}
static Model()
{
// Set up an empty event handler
FilterStringChanged += (sender, e) => { return; };
}
}
}
But now the same property bind to DataGrid
. How can I bind?
I think what you ware looking for is
MultiBinding
:Here is the
NameConverter
:NameList
:PersonName
:Here is a link to the full source code, just go into the
Samples
->Data Binding
->MultiBinding
project in theWPFSamples.sln
:https://drive.google.com/drive/folders/0BxL9JBEAEaAUcjlxNWdzU3h6bjA?resourcekey=0-N-piwEgxrfTw4CT-4cnEqA&usp=sharing
For details please see:
https://learn.microsoft.com/en-us/previous-versions/ms771633(v=vs.100)?redirectedfrom=MSDN