Cubemaster
Cubemaster

Reputation: 527

Can't see Textbox text in DataGrid

I am trying to make a DataGrid in WPF that will display the different properties of the items in a given collection. So far, everything works perfectly, except for one. Here is an image of my DataGrid, which is fully populated just as intended:

Fully Populated Data Grid

And you may already see my problem. Each and every item has a value associated with it, which is properly binded. However, I cannot see the value for any of the items unless I specifically click on that cell. For example:

Data Grid With Value Showing

I want the values to be visible at all times, just like the Name and Default Value Columns, but it is very important that the value, and only the value, is editable from the DataGrid at runtime. Here is the XAML I have in place for the datagrid:

<DataGrid Margin="20" AutoGenerateColumns="False" ItemsSource="{Binding ConfigurationParameterCollection}" Name="MasterListBox" ScrollViewer.VerticalScrollBarVisibility="Visible" SelectionChanged="MasterListBox_SelectionChanged" AlternationCount="2" DockPanel.Dock="Top" HorizontalAlignment="Center" Width="1000" >
        <DataGrid.Columns>

            <DataGridTextColumn Header="Name" Binding="{Binding Name}"/>

            <!-- Begin Problem Area-->

            <DataGridTemplateColumn Header="Value">
                <DataGridTemplateColumn.CellEditingTemplate>
                    <DataTemplate>
                        <TextBox Text="{Binding Path=Value, Mode=TwoWay}" />
                    </DataTemplate>
                </DataGridTemplateColumn.CellEditingTemplate>
            </DataGridTemplateColumn>

            <!-- End Problem Area-->

            <DataGridTextColumn  Header="Default Value" Binding="{Binding DefaultValue}"/>


        </DataGrid.Columns>
    </DataGrid>

Don't worry about the name of the DataGrid, it used to be a ListBox and I just never bothered to change the name. Everything links up perfectly, I just can't figure out why the TextBox text is not visible.

Any help you can give is much appreciated.

EDIT

I know that I can achieve this with using CellTemplate instead of CellEditingTemplate, but the latter has the feature of only editing on two clicks, but selecting the row otherwise. I want to have this feature, so If there is a way to do that with CellTemplate please let me know. With CellTemplate, I see the boxes at all times:

enter image description here

I want it to be like in the second picture, where the rest of the column appears in style with the datagrid, while the cell I am editing looks like a text box.

Upvotes: 2

Views: 1000

Answers (1)

Change DataGridTemplateColumn.CellEditingTemplate To DataGridTemplateColumn.CellTemplate


You can specify a display template, and an edit template ...

        <DataGridTemplateColumn Header="Value">
            <DataGridTemplateColumn.CellEditingTemplate>
                <DataTemplate>
                    <TextBox Text="{Binding Path=Value, Mode=TwoWay}" />
                </DataTemplate>
            </DataGridTemplateColumn.CellEditingTemplate>
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Path=Value, Mode=TwoWay}" />
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>

Upvotes: 2

Related Questions