2

I have a data grid whose width is initialized to the width of the row that it is in in the parent grid. The columns don't fill all of the empty space in the grid, what I want is for each column to have an equal width. A coworker of mine set the ColumnWidth property of the datagrid to "*" which is supposed to work (it works in another application that he created but not when he opens my solution on his computer, nor does it work on my computer.) See the attached picture for what the columns look like.enter image description here

I would imagine that the property should work for me as well, but it doesn't— the width of each column is only one letter wide. Does it have something to do with me binding to an Observable Collection of objects? Also, since I couldn't get the ColumnWidth property to work, I tried creating a multivalue converter that takes two parameters, the width of the data grid and the number of columns that I have. I can't get that converter to fire off when my view loads. I have used multibinding converters before with booleans, but again not with Observable Collections. Could that be the issue? Any help would be greatly appreciated.

<DataGrid x:Name="DataGrid" Grid.Row="7" Grid.Column="3" AutoGenerateColumns="False" FontSize="18" ItemsSource="{Binding ObservableCollectionInViewModel}">
        <DataGrid.Columns>
            <DataGridTextColumn Binding="{Binding Path=PartNumber}" Header="Part Number">
                <DataGridTextColumn.Width>
                    <MultiBinding Converter="{StaticResource DataGridEvenColumnConverter}">
                        <Binding Path="DataGridWidth"/> <!--{Binding ElementName=DataGrid, Path=Width}-->
                        <Binding Path="ColumnCount"/>
                    </MultiBinding>
                </DataGridTextColumn.Width>
            </DataGridTextColumn>
            <DataGridTextColumn Binding="{Binding Path=SerialNumber}" Header="Serial Number">
                <DataGridTextColumn.Width>
                    <MultiBinding Converter="{StaticResource DataGridEvenColumnConverter}">
                        <Binding Path="DataGridWidth"/>
                        <Binding Path="ColumnCount"/>
                    </MultiBinding>
                </DataGridTextColumn.Width>
            </DataGridTextColumn>
            <DataGridTextColumn Binding="{Binding Path=Weight}" Header="Weight">
                <DataGridTextColumn.Width>
                    <MultiBinding Converter="{StaticResource DataGridEvenColumnConverter}">
                        <Binding Path="DataGridWidth"/>
                        <Binding Path="ColumnCount"/>
                    </MultiBinding>
                </DataGridTextColumn.Width>
            </DataGridTextColumn>
            <DataGridTextColumn Binding="{Binding Path=Quantity}" Header="Quantity">
                <DataGridTextColumn.Width>
                    <MultiBinding Converter="{StaticResource DataGridEvenColumnConverter}">
                        <Binding Path="DataGridWidth"/>
                        <Binding Path="ColumnCount"/>
                    </MultiBinding>
                </DataGridTextColumn.Width>
            </DataGridTextColumn>
        </DataGrid.Columns>
    </DataGrid>
  • These are not valid paths for a Binding: `[Column Count]`, `[DataGridWidth]` – 15ee8f99-57ff-4f92-890c-b56153 Dec 01 '17 at 14:42
  • Sorry I didn't mean for those to be there. I changed them in my update. I did not have those in my current project and it still didn't fire. – SQL and Java Learner Dec 01 '17 at 14:47
  • What are they properties of? – 15ee8f99-57ff-4f92-890c-b56153 Dec 01 '17 at 14:47
  • 1
    There should be no problem with setting `width="*"` for your columns. It sounds like an external factor (container, initialization) is the problem. As for your converter, assuming you have `DataGridWidth` and `ColumnCount` in your viewmodel or otherwise; make sure to add the `DataContext.` prefix to the properties to ensure the values are correctly located, which can be an issue within `ItemSources` and `DataTemplates`. Perhaps setting a `RelativeResource` to the Window or UserControl to ensure the correct DataContext is set. – Jack Dec 01 '17 at 14:51
  • Using the "DataContext." prefix allowed the property to be found in the view model, so that's awesome. Do you have any insight as to what that prefix does? Thanks again. – SQL and Java Learner Dec 01 '17 at 14:56
  • You can set a DataContext for mostly any control, including the Window or a User Control. Usually, the Window / User Control will be set as the default DataContext. Once you set an ItemSource however, you are effectively changing the DataContext for all child controls of the ItemSource. From what I can gather, setting the `DataContext.` prefix tells the control to use the default DataContext. You can also set the `RelativeSource` of a binding to tell it where the context is located in terms of Ancestor Level and Type, useful for accessing parent context properties quickly and easily. – Jack Dec 01 '17 at 15:03
  • Maybe the best route to take with your issue is to figure out why `width="*"` isn't working. What can you tell us about the parent container of your Datagrid? How do you initialize the width? – Jack Dec 01 '17 at 15:09
  • @Jack *"From what I can gather, setting the DataContext. prefix tells the control to use the default DataContext."* -- Not sure what you mean there, but there's nothing special about that string other than Binding using the local control's DataContext property as a source by default. – 15ee8f99-57ff-4f92-890c-b56153 Dec 01 '17 at 15:13
  • My mistake, thanks for the correction. Best thing to do is to research these topics from trustworthy sources, I do not know the details well. From what you said however, I don't understand why adding `DataContext.` solves the issue of locating the property source. – Jack Dec 01 '17 at 15:16
  • The parent container is the main window's default grid, so there's nothing too crazy like a deeply nested grid or properties on that parent grid. I'll look into some more to see if I can come up with something but that is the only parent container that I have. – SQL and Java Learner Dec 01 '17 at 15:23
  • You're asking how to bind to a property of an unknown object. What is it? where is it? Only you know. – 15ee8f99-57ff-4f92-890c-b56153 Dec 01 '17 at 16:07

0 Answers0