locked
How to change the width of the width of the datagrid with a property in my view model, converter or data trigger? RRS feed

  • Question

  • I would like to change the width of a data trigger according to the value of a property in my view model, so I tried this:

    <DataGridTextColumn Header="MyColumn" Binding="{Binding Myproperty}"
    	Width="{Binding MypropertyInViewModel}">

    But when I set the property in my view model the width of the column is not changed. I have tried a propery of type string with value "2cm", I have tried the type doulbe with value 10d but neither of this works.

    So I would like to know if there is any way to change the width of the column by code. Any solution it is good, to bind to a property in my view model, data trigger, converter or any other solution.

    Really what I want to do is to set the width of the dataGrid according to the value of a bool property in my view model. If the value is true, the width has to be 2.5cm, if the value is false, the value has to be 7cm.

    Thanks.

    Sunday, January 27, 2019 9:21 AM

Answers

  • Hi ComptonAlvaro,

    As I reply before, WPF knows how to parse something like the Binding property correctly and transfer the binding to each Cell, however something like Width simply gets evaluated as-is, and does not evaluate correctly because neither the DataContext nor VisualTree is there as expected.

    So I suggest you can use DataGridTemplateColumn to change width property using binding.

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, January 30, 2019 7:39 AM

All replies

  • Width is a double , not sure how you set a double to 10d.

    https://docs.microsoft.com/en-us/dotnet/api/system.windows.frameworkelement.width?view=netframework-4.7.2


    Lloyd Sheen

    Sunday, January 27, 2019 1:04 PM
  • I set my double property to 12 instead of 12d, but the width of the column doesn't change when I change the value of this property.
    Sunday, January 27, 2019 3:50 PM
  • Without seeing the definition of the property and the XAML for the column it is difficult to help.

    If you change the value of a property and it does not change in the UI then I would think you have binding problems.


    Lloyd Sheen

    Sunday, January 27, 2019 3:58 PM
  • The property of my view model is this:

    private double _dummy = 300;
            public double Dummy
            {
                get { return _dummy; }
                set
                {
                    _dummy = value;
                    base.RaisePropertyChangedEvent("Dummy");
                }
            }

    The definition of my column in my axml is this:

    <DataGridTextColumn Header="MyColumn" Binding="{Binding MyPropertyInData}"
                                            Width="{Binding ElementName=ucMyView, Path=DataContext.Dummy}">

    Where "ucMyView" in my width binding is the name of my user control.


    Sunday, January 27, 2019 4:31 PM
  • Hi ComptonAlvaro,

    The DataGridTextColumn is an abstract object that isn't actually part of the VisualTree, so it doesn't make use of an inherited DataContext like you would expect with other controls.

    WPF knows how to parse something like the Binding property correctly and transfer the binding to each Cell, however something like Width simply gets evaluated as-is, and does not evaluate correctly because neither the DataContext nor VisualTree is there as expected.

    I suggest you can conside to use DataGridTemplateColumn,with a control that has it's Width bound to whatever the property is on your DataItem

     <DataGridTemplateColumn Header="column2">
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <StackPanel>
                                    <TextBlock Width="{Binding ElementName=textbox1, Path=Text}" Text="{Binding firstname}" />
                                </StackPanel>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>
    
     <TextBox
                Name="textbox1"
                Width="200"
                Height="30"
                Margin="10"
                Text="100" />

    There are some thread , you can take a look:

    https://stackoverflow.com/questions/30241116/how-to-bind-datagrid-columns-width-in-wpf-mvvm

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Monday, January 28, 2019 9:01 AM
  • I am take a look to the link that you offer me, and it was the solution that I am using to set the visibility with a converter, but the width doesn't fire the converter.

    In the case of the visibility I have this code:

    <UserControl.Resources>
            <ResourceDictionary>
    			<FrameworkElement x:Key="ProxyElement" DataContext="{Binding}" />
    		 </ResourceDictionary>
    </UserControl.Resources>
    
    
    
    <DataGridTextColumn Header="MyCOlumn" Binding="{Binding MyData}" Width="2.5cm"
    	Visibility="{Binding DataContext.MyBoolPropertyInViewModel, Source={StaticResource ProxyElement}, Converter={StaticResource bool2VisibilityConverter}}"
    </DataGridTextColumn>

    But if I try to use the same code for the width with this code it deoesn't work, the converter it is not fired.

    Width="{Binding DataContext.MyBoolPropertyInViewModel, Source={StaticResource ProxyElement}, Converter={StaticResource myConverterForWidth}}"

    The width use the same bool property than visibility to set the width value.

    However, if I only change width by Visibility, the converter for the width it is fired, so I don't undertand why the visibility can fire the converter but the width property can't.

    Monday, January 28, 2019 12:55 PM
  • Hi ComptonAlvaro,

    As I reply before, WPF knows how to parse something like the Binding property correctly and transfer the binding to each Cell, however something like Width simply gets evaluated as-is, and does not evaluate correctly because neither the DataContext nor VisualTree is there as expected.

    So I suggest you can use DataGridTemplateColumn to change width property using binding.

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, January 30, 2019 7:39 AM