Click here to Skip to main content
15,895,423 members

Problem scroll bar and column width of data grid

Schwimo asked:

Open original thread
XML
<Grid>
    <DataGrid AutoGenerateColumns="False"
              ItemsSource="{Binding Path=Database}">
        <DataGrid.Columns>
            <DataGridTextColumn Width="Auto"
                                Binding="{Binding Path=Integer}"
                                Header="String" />
            <DataGridTextColumn Width="Auto"
                                Binding="{Binding Path=String}"
                                Header="String" />
        </DataGrid.Columns>
    </DataGrid>
</Grid>


This is my Xaml example code to reproduce the problem... there is no code behind.

C#
class DataGridViewModel
    {
        public class Data
        {
            #region properties
            public int Integer { get; set; }
            public string String { get; set; }
            public double Double { get; set; }
            #endregion

            #region instance constructors
            /// <summary>
            /// Initializes a new instance of the <see cref="Data"/> class.
            /// </summary>
            public Data(int i, double d, string s)
            {
                /* initializes all instance members */
                Integer = i;
                Double = d;
                String = s;
            }
            #endregion
        }

        private readonly List<Data> _database = new List<Data>(10);

        public IEnumerable<Data> Database
        {
            get { return _database; }
        }

        public DataGridViewModel()
        {
            /* initializes all instance members */
            _database.Add(new Data(1, 1.5, "test1test1test1test1test1test1test1test1test1test1test1test1test1test1test1test1test1test1"));
            _database.Add(new Data(2, 2.5, "test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2test2"));
            _database.Add(new Data(3, 3.5, "test3test3test3test3test3test3test3test3test3test3test3test3test3test3test3test3test3test3"));
            _database.Add(new Data(4, 4.5, "test4test4test4test4test4test4test4test4test4test4test4test4test4test4test4test4test4test4"));
            _database.Add(new Data(5, 5.5, "test5test5test5test5test5test5test5test5test5test5test5test5test5test5test5test5test5test5"));
            _database.Add(new Data(6, 6.5, "test6test6test6test6test6test6test6test6test6test6test6test6test6test6test6test6test6test6"));
            _database.Add(new Data(7, 7.5, "test7test7test7test7test7test7test7test7test7test7test7test7test7test7test7test7test7test7"));
            _database.Add(new Data(8, 8.5, "test8test8test8test8test8test8test8test8test8test8test8test8test8test8test8test8test8test8"));
            _database.Add(new Data(9, 9.5, "test9test9test9test9test9test9test9test9test9test9test9test9test9test9test9test9test9test9"));
            _database.Add(new Data(10, 10.5, "test10test10test10test10test10test10test10test10test10test10test10test10test10test10test10"));
        }
    }


this is the view model.


Problem: If I set the column width to "auto" the horizontal scroll bar shows as wished but if the window gets to big i can't select a row in the last empty column.

If I set the column width to "*" the empty column is removed and i can select a row everywhere in the data grid. Now I have a Problem with the horizontal scroll bar, which isn't shown or if it gets shown some times i can't scroll and my text gets cut.

Greetings
Tags: C#, WPF, MVVM, DataGrid

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900