Binding to Properties not work in CodeBehind but work in xaml [duplicate]












0
















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:




  1. In the properties create value
    enter image description here

  2. 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>



  1. 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?










share|improve this question















marked as duplicate by ASh, Clemens c#
Users with the  c# badge can single-handedly close c# questions as duplicates and reopen them as needed.

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 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











  • ASh, Thank! Excellent observation and correct assumption

    – Александр Пекшев
    Nov 14 '18 at 18:59
















0
















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:




  1. In the properties create value
    enter image description here

  2. 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>



  1. 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?










share|improve this question















marked as duplicate by ASh, Clemens c#
Users with the  c# badge can single-handedly close c# questions as duplicates and reopen them as needed.

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 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











  • ASh, Thank! Excellent observation and correct assumption

    – Александр Пекшев
    Nov 14 '18 at 18:59














0












0








0









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:




  1. In the properties create value
    enter image description here

  2. 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>



  1. 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?










share|improve this question

















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:




  1. In the properties create value
    enter image description here

  2. 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>



  1. 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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 14 '18 at 18:09









ASh

21.9k33060




21.9k33060










asked Nov 14 '18 at 17:56









Александр ПекшевАлександр Пекшев

345




345




marked as duplicate by ASh, Clemens c#
Users with the  c# badge can single-handedly close c# questions as duplicates and reopen them as needed.

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 c#
Users with the  c# badge can single-handedly close c# questions as duplicates and reopen them as needed.

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 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











  • ASh, Thank! Excellent observation and correct assumption

    – Александр Пекшев
    Nov 14 '18 at 18:59














  • 1





    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











  • 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












1 Answer
1






active

oldest

votes


















0














Must be:



BindingOperations.SetBinding(propertyNameColumnDefinition, ColumnDefinition.WidthProperty, CreateBindingForColumnWidth());


Thank for ASh for his comment






share|improve this answer






























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    Must be:



    BindingOperations.SetBinding(propertyNameColumnDefinition, ColumnDefinition.WidthProperty, CreateBindingForColumnWidth());


    Thank for ASh for his comment






    share|improve this answer




























      0














      Must be:



      BindingOperations.SetBinding(propertyNameColumnDefinition, ColumnDefinition.WidthProperty, CreateBindingForColumnWidth());


      Thank for ASh for his comment






      share|improve this answer


























        0












        0








        0







        Must be:



        BindingOperations.SetBinding(propertyNameColumnDefinition, ColumnDefinition.WidthProperty, CreateBindingForColumnWidth());


        Thank for ASh for his comment






        share|improve this answer













        Must be:



        BindingOperations.SetBinding(propertyNameColumnDefinition, ColumnDefinition.WidthProperty, CreateBindingForColumnWidth());


        Thank for ASh for his comment







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 14 '18 at 19:01









        Александр ПекшевАлександр Пекшев

        345




        345

















            Popular posts from this blog

            The Sandy Post

            Danny Elfman

            Pages that link to "Head v. Amoskeag Manufacturing Co."