Binding Text Descriptions to TextBoxes - c#

I have a problem using C# and WPF of binding Text to TextBoxes in a Grid. The binding actually works, except that it doesn't show up on the UI until I double click in the TextBox to edit. Then the text fills in. I'm using DataGridTemplateColumn.CellEditingTemplate. I've noticed that if I just use CellTemplate, the data fills in (but obviously I can't change it). Can anyone tell me what must be different? I'll post code if needed.

If you simply want to bind a text column then you can do this.
<DataGridTextColumn Header="Name" Binding="{Binding Name}"/>
If you have something more complicated in mind with a DataGridTemplateColumn, then you may need a to specify a CellTemplate and a CellEditingTemplate. You'll need to describe what your trying to do, and show the Xaml.

Related

Dynamic change of DatagridTextColumn path WPF, MVVM

I read a Excel file to a System.Data.Datatable and display it on DataGrid. Everything works fine if columns are created automatically or I know DataGridTextColumns Property path.
<DataGrid Grid.Row="1" ItemsSource="{Binding ExcelTable}" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Header="Reference" Binding="{Binding Path=ColumnHeader}"></DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
Problem is that I do not know what the path will be, because properties are created after opening a file.I would like to create a path e,g, ColumnHeader And change it dynamically during a runtime.
It is easy to dynamically change string value of an textBox with a help of TryGetMember and so on. But with this one I do not know how it would work, since ItemSource is the one that raises TryGetMember, not the column.
Is it possible to do it this way? I know other ways, but when I already have a dataTable...

DataGridTemplateColumn Column Span in DataGrid WPF

I am making UI as shown in picture below.
It is a DataGrid, Itemsource is bound to a List. I cannot set ColumnSpan for the TextBox. First, I tried it with a UserControl but I couldn't fix DataGridTemplateColumn Header, then I tried DataGridTemplateColumn.CellTemplate but couldn't set the ColumnSpan.
Is there any way/method to make this kind of Datagrid?
Thank you in advance.
There is no ColumnSpan in the DataGrid, like in the normal Grid.
To get this kind of layout you have 3 choices, all with heavy drawbacks unfortunately.
1 merge cells
Use the merge event to merge cells together in code behind. See a
guide to this here This requires a large chunk of code behind
coding to get this right with your layout. So I would really advise
against this.
2 RowDetails
Use the build in RowDetails. This doesn't get the layout exactly like your's but is the easiest and closest build in function to your requirement.
It will look something like this:
It's easy to configure:
Set: RowDetailsVisibilityMode="Visible" in the DataGrid XAML.
And define your template for the row:
<DataGrid.RowDetailsTemplate>
<DataTemplate>
<TextBlock Text="{Binding Desc}" Background="LightGray"/>
</DataTemplate>
</DataGrid.RowDetailsTemplate>
[+] All DataGrid functions will work: Sorting, Adding Rows, Resizing ...
[-] The Details section span over the whole row.
See this tutorial about what you can do with row details.
3 Hack something together
With templates and code behindz. Some resources that might help you:
Override Row Template
Build a custom cell template
DataGrid in general
I have come with other solution using Grids, List and UserControl. In a simple way, I made it as following:
DataGrid is not suitable for what you want.
You had better use ListView or ListBox and datatemplate. By using them, you can show every kind of data in the way you want without DataGrid.
If you are not familiar with listview and datatemplete, please see the link below.
https://www.wpftutorial.net/ListBoxDataTemplate.html

Autosize to fit All Content

In WPF, I have the following:
<ListView.View>
<GridView>
<GridViewColumn
Width="Auto"
DisplayMemberBinding="{Binding SomeProperty, Mode=OneWay}"
Header="Type"/>
The problem is that this only auto-sizes the column to the visible content. This is the same behavior when double clicking the header divider as well.
I want it to resize the column according to all content. For contrast, take the Winforms DataGridView equivalent to the current Auto resizing behavior and the equivalent to the desired behavior:
this.dataGridView1.Columns[0].AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells; // Current behavior
this.dataGridView1.Columns[0].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells; // Desired behavior
Is this possible for a WPF ListView?
This question is different than How to autosize and right-align GridViewColumn data in WPF? and others like it because they still trigger the current resizing behavior for visible content and do not address all content.
The problem is the ListView has its ItemsPanel with virtualizing enabled by default. In this mode, only visible content will be rendered and then will trigger the auto-sizing column. So if you don't have many items in your ListView, you can turn off the virtualizing mode via the attached property VirtualizingStackPanel.IsVirtualizing, then it will work expectedly:
<ListView VirtualizingStackPanel.IsVirtualizing="false">
<!-- ... -->
</ListView>
If you don't want to turn off virtualizing, then I don't think there is some easy way to take benefit of the Width="Auto". You may have to implement that feature manually yourself. And technically we need to have knowledge about the largest width to update the column's width accordingly. We can make some for loop every time we need to update the column's width but this is expensive. We can also manage to store the largest width every time a cell has its content changed (this is the better approach). Anyway doing it manually is a pain.

invalid operation exception when the column is a check box and use a converter

I have a datagrid and one of the columns are ths:
<DataGridCheckBoxColumn Header="MyColumn" Binding="{Binding ConverterParameter=this, Converter={StaticResource ResourceKey=myCOnverterValueConverter}}" />
If I use a text column I don't have any problem, but if I change it to check box column I get the exception invalid operation exception, and the code does not get the converter code.
If I use a check box column and I don't use any converter I don't have any problem.
How can I use a converter with check box columns in a dataGrid?.
Thanks.
I doubt the problem associated is Mode property of the Binding which is TwoWay by default and since you are binding it to ConverterParameter you should set the this to OneWay.
If you need how you can implement TwoWay binding read the Editing data section which provide a nice explanation about it.
The above article also explains how we can use ConverterParameter correctly with DataGridCheckBoxColumn in the Displaying data section.
Just try this hope it works,
<DataGridCheckBoxColumn Header="MyColumn" Binding="{Binding ConverterParameter=this, Mode=OneWay, Converter={StaticResource ResourceKey=myCOnverterValueConverter}}" />

WPF DataGrid AutoGenerated columns, change header name

I recall in Silverlight the ability to place an Attribute on a given property in the Model for an alternate display name when auto generating columns on a data grid. Is this possible in WPF? I don't want to use the event handler to change the names.
Found it...here is what I was referencing DisplayAttribute, however that does not appear valid in WPF, only SL. For WPF it can be done like this...keeping everything in XAML...
<dg:DataGrid AutoGenerateColumns="False" ItemsSource="{Binding DatabaseConnections, Mode=Default}">
<dg:DataGrid.Columns>
<dg:DataGridTextColumn Header="Display" Binding="{Binding DisplayName}"></dg:DataGridTextColumn>
</dg:DataGrid.Columns>
</dg:DataGrid>
...this allows you to change the DisplayName property to get displayed as "Display" in the header of the DataGrid.
Check out: How to: Customize Auto-Generated Columns in the DataGrid Control.

Categories

Resources