Binding to Properties not work in CodeBehind but work in xaml [duplicate]
This question already has an answer here:
How to programmatically binding width DataGridColumn from another DataGridColumn
2 answers
I needed to make the columns in different Grids move dependently on each other. I found a good solution:
- In the properties create value

- In the xamlI create a binding:
<Grid.ColumnDefinitions>
<ColumnDefinition
Width="Binding Source=x:Static properties:Settings.Default, Path=GridColumnWidth,
Converter=StaticResource ColumnWidthConverter, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged"
MinWidth="50"></ColumnDefinition>
<ColumnDefinition Width="Auto"></ColumnDefinition>
<ColumnDefinition Width="*" MinWidth="50"></ColumnDefinition>
</Grid.ColumnDefinitions>
- And implement converter:
public class ColumnWidthConverter : IValueConverter
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
if (value is string str && !str.Equals("*"))
return new GridLength(double.Parse(str));
return new GridLength(1, GridUnitType.Star);
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
if (value is GridLength gridLength)
return gridLength.Value.ToString(CultureInfo.InvariantCulture);
return "*";
Now I needed to do the same in CodeBehind. I do this:
ColumnDefinition propertyNameColumnDefinition = new ColumnDefinition();
propertyNameColumnDefinition.MinWidth = 50;
BindingOperations.SetBinding(propertyNameColumnDefinition, WidthProperty, CreateBindingForColumnWidth());
ColumnDefinition gridSplitterColumnDefinition = new ColumnDefinition() Width = GridLength.Auto ;
ColumnDefinition propertyValueColumnDefinition = new ColumnDefinition();
propertyValueColumnDefinition.MinWidth = 50;
grid.RowDefinitions.Add(headerRowDefinition);
grid.ColumnDefinitions.Add(propertyNameColumnDefinition);
grid.ColumnDefinitions.Add(gridSplitterColumnDefinition);
grid.ColumnDefinitions.Add(propertyValueColumnDefinition);
.......
private Binding CreateBindingForColumnWidth()
Binding b = new Binding
Source = mpESKD.Properties.Settings.Default,
Path = new PropertyPath("GridColumnWidth"),
Mode = BindingMode.TwoWay,
UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged,
Converter = new ColumnWidthConverter()
;
return b;
In this case, nothing works.
What am I doing wrong?
c# wpf xaml binding
marked as duplicate by ASh, Clemens
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Nov 14 '18 at 20:16
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
How to programmatically binding width DataGridColumn from another DataGridColumn
2 answers
I needed to make the columns in different Grids move dependently on each other. I found a good solution:
- In the properties create value

- In the xamlI create a binding:
<Grid.ColumnDefinitions>
<ColumnDefinition
Width="Binding Source=x:Static properties:Settings.Default, Path=GridColumnWidth,
Converter=StaticResource ColumnWidthConverter, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged"
MinWidth="50"></ColumnDefinition>
<ColumnDefinition Width="Auto"></ColumnDefinition>
<ColumnDefinition Width="*" MinWidth="50"></ColumnDefinition>
</Grid.ColumnDefinitions>
- And implement converter:
public class ColumnWidthConverter : IValueConverter
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
if (value is string str && !str.Equals("*"))
return new GridLength(double.Parse(str));
return new GridLength(1, GridUnitType.Star);
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
if (value is GridLength gridLength)
return gridLength.Value.ToString(CultureInfo.InvariantCulture);
return "*";
Now I needed to do the same in CodeBehind. I do this:
ColumnDefinition propertyNameColumnDefinition = new ColumnDefinition();
propertyNameColumnDefinition.MinWidth = 50;
BindingOperations.SetBinding(propertyNameColumnDefinition, WidthProperty, CreateBindingForColumnWidth());
ColumnDefinition gridSplitterColumnDefinition = new ColumnDefinition() Width = GridLength.Auto ;
ColumnDefinition propertyValueColumnDefinition = new ColumnDefinition();
propertyValueColumnDefinition.MinWidth = 50;
grid.RowDefinitions.Add(headerRowDefinition);
grid.ColumnDefinitions.Add(propertyNameColumnDefinition);
grid.ColumnDefinitions.Add(gridSplitterColumnDefinition);
grid.ColumnDefinitions.Add(propertyValueColumnDefinition);
.......
private Binding CreateBindingForColumnWidth()
Binding b = new Binding
Source = mpESKD.Properties.Settings.Default,
Path = new PropertyPath("GridColumnWidth"),
Mode = BindingMode.TwoWay,
UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged,
Converter = new ColumnWidthConverter()
;
return b;
In this case, nothing works.
What am I doing wrong?
c# wpf xaml binding
marked as duplicate by ASh, Clemens
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Nov 14 '18 at 20:16
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
1
please check whichWidthPropertyis used. tryColumnDefinition.WidthProperty. see similar case
– ASh
Nov 14 '18 at 18:07
is converter method triggered, and if yes which value it receives?
– ASh
Nov 14 '18 at 18:13
ASh, Thank! Excellent observation and correct assumption
– Александр Пекшев
Nov 14 '18 at 18:59
add a comment |
This question already has an answer here:
How to programmatically binding width DataGridColumn from another DataGridColumn
2 answers
I needed to make the columns in different Grids move dependently on each other. I found a good solution:
- In the properties create value

- In the xamlI create a binding:
<Grid.ColumnDefinitions>
<ColumnDefinition
Width="Binding Source=x:Static properties:Settings.Default, Path=GridColumnWidth,
Converter=StaticResource ColumnWidthConverter, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged"
MinWidth="50"></ColumnDefinition>
<ColumnDefinition Width="Auto"></ColumnDefinition>
<ColumnDefinition Width="*" MinWidth="50"></ColumnDefinition>
</Grid.ColumnDefinitions>
- And implement converter:
public class ColumnWidthConverter : IValueConverter
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
if (value is string str && !str.Equals("*"))
return new GridLength(double.Parse(str));
return new GridLength(1, GridUnitType.Star);
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
if (value is GridLength gridLength)
return gridLength.Value.ToString(CultureInfo.InvariantCulture);
return "*";
Now I needed to do the same in CodeBehind. I do this:
ColumnDefinition propertyNameColumnDefinition = new ColumnDefinition();
propertyNameColumnDefinition.MinWidth = 50;
BindingOperations.SetBinding(propertyNameColumnDefinition, WidthProperty, CreateBindingForColumnWidth());
ColumnDefinition gridSplitterColumnDefinition = new ColumnDefinition() Width = GridLength.Auto ;
ColumnDefinition propertyValueColumnDefinition = new ColumnDefinition();
propertyValueColumnDefinition.MinWidth = 50;
grid.RowDefinitions.Add(headerRowDefinition);
grid.ColumnDefinitions.Add(propertyNameColumnDefinition);
grid.ColumnDefinitions.Add(gridSplitterColumnDefinition);
grid.ColumnDefinitions.Add(propertyValueColumnDefinition);
.......
private Binding CreateBindingForColumnWidth()
Binding b = new Binding
Source = mpESKD.Properties.Settings.Default,
Path = new PropertyPath("GridColumnWidth"),
Mode = BindingMode.TwoWay,
UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged,
Converter = new ColumnWidthConverter()
;
return b;
In this case, nothing works.
What am I doing wrong?
c# wpf xaml binding
This question already has an answer here:
How to programmatically binding width DataGridColumn from another DataGridColumn
2 answers
I needed to make the columns in different Grids move dependently on each other. I found a good solution:
- In the properties create value

- In the xamlI create a binding:
<Grid.ColumnDefinitions>
<ColumnDefinition
Width="Binding Source=x:Static properties:Settings.Default, Path=GridColumnWidth,
Converter=StaticResource ColumnWidthConverter, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged"
MinWidth="50"></ColumnDefinition>
<ColumnDefinition Width="Auto"></ColumnDefinition>
<ColumnDefinition Width="*" MinWidth="50"></ColumnDefinition>
</Grid.ColumnDefinitions>
- And implement converter:
public class ColumnWidthConverter : IValueConverter
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
if (value is string str && !str.Equals("*"))
return new GridLength(double.Parse(str));
return new GridLength(1, GridUnitType.Star);
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
if (value is GridLength gridLength)
return gridLength.Value.ToString(CultureInfo.InvariantCulture);
return "*";
Now I needed to do the same in CodeBehind. I do this:
ColumnDefinition propertyNameColumnDefinition = new ColumnDefinition();
propertyNameColumnDefinition.MinWidth = 50;
BindingOperations.SetBinding(propertyNameColumnDefinition, WidthProperty, CreateBindingForColumnWidth());
ColumnDefinition gridSplitterColumnDefinition = new ColumnDefinition() Width = GridLength.Auto ;
ColumnDefinition propertyValueColumnDefinition = new ColumnDefinition();
propertyValueColumnDefinition.MinWidth = 50;
grid.RowDefinitions.Add(headerRowDefinition);
grid.ColumnDefinitions.Add(propertyNameColumnDefinition);
grid.ColumnDefinitions.Add(gridSplitterColumnDefinition);
grid.ColumnDefinitions.Add(propertyValueColumnDefinition);
.......
private Binding CreateBindingForColumnWidth()
Binding b = new Binding
Source = mpESKD.Properties.Settings.Default,
Path = new PropertyPath("GridColumnWidth"),
Mode = BindingMode.TwoWay,
UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged,
Converter = new ColumnWidthConverter()
;
return b;
In this case, nothing works.
What am I doing wrong?
This question already has an answer here:
How to programmatically binding width DataGridColumn from another DataGridColumn
2 answers
c# wpf xaml binding
c# wpf xaml binding
edited Nov 14 '18 at 18:09
ASh
21.9k33161
21.9k33161
asked Nov 14 '18 at 17:56
Александр ПекшевАлександр Пекшев
345
345
marked as duplicate by ASh, Clemens
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Nov 14 '18 at 20:16
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by ASh, Clemens
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Nov 14 '18 at 20:16
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
1
please check whichWidthPropertyis used. tryColumnDefinition.WidthProperty. see similar case
– ASh
Nov 14 '18 at 18:07
is converter method triggered, and if yes which value it receives?
– ASh
Nov 14 '18 at 18:13
ASh, Thank! Excellent observation and correct assumption
– Александр Пекшев
Nov 14 '18 at 18:59
add a comment |
1
please check whichWidthPropertyis used. tryColumnDefinition.WidthProperty. see similar case
– ASh
Nov 14 '18 at 18:07
is converter method triggered, and if yes which value it receives?
– ASh
Nov 14 '18 at 18:13
ASh, Thank! Excellent observation and correct assumption
– Александр Пекшев
Nov 14 '18 at 18:59
1
1
please check which
WidthProperty is used. try ColumnDefinition.WidthProperty. see similar case– ASh
Nov 14 '18 at 18:07
please check which
WidthProperty is used. try ColumnDefinition.WidthProperty. see similar case– ASh
Nov 14 '18 at 18:07
is converter method triggered, and if yes which value it receives?
– ASh
Nov 14 '18 at 18:13
is converter method triggered, and if yes which value it receives?
– ASh
Nov 14 '18 at 18:13
ASh, Thank! Excellent observation and correct assumption
– Александр Пекшев
Nov 14 '18 at 18:59
ASh, Thank! Excellent observation and correct assumption
– Александр Пекшев
Nov 14 '18 at 18:59
add a comment |
1 Answer
1
active
oldest
votes
Must be:
BindingOperations.SetBinding(propertyNameColumnDefinition, ColumnDefinition.WidthProperty, CreateBindingForColumnWidth());
Thank for ASh for his comment
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Must be:
BindingOperations.SetBinding(propertyNameColumnDefinition, ColumnDefinition.WidthProperty, CreateBindingForColumnWidth());
Thank for ASh for his comment
add a comment |
Must be:
BindingOperations.SetBinding(propertyNameColumnDefinition, ColumnDefinition.WidthProperty, CreateBindingForColumnWidth());
Thank for ASh for his comment
add a comment |
Must be:
BindingOperations.SetBinding(propertyNameColumnDefinition, ColumnDefinition.WidthProperty, CreateBindingForColumnWidth());
Thank for ASh for his comment
Must be:
BindingOperations.SetBinding(propertyNameColumnDefinition, ColumnDefinition.WidthProperty, CreateBindingForColumnWidth());
Thank for ASh for his comment
answered Nov 14 '18 at 19:01
Александр ПекшевАлександр Пекшев
345
345
add a comment |
add a comment |
1
please check which
WidthPropertyis used. tryColumnDefinition.WidthProperty. see similar case– ASh
Nov 14 '18 at 18:07
is converter method triggered, and if yes which value it receives?
– ASh
Nov 14 '18 at 18:13
ASh, Thank! Excellent observation and correct assumption
– Александр Пекшев
Nov 14 '18 at 18:59