开发者

WPF DataGrid cell outlined in Red? (Noobie)

开发者 https://www.devze.com 2023-02-14 12:23 出处:网络
I will start by saying that I am completely new to WPF. But, I do have 20 years Development experience and about 5 years of C# webform experience.

I will start by saying that I am completely new to WPF. But, I do have 20 years Development experience and about 5 years of C# webform experience.

I am trying to diagnose a WPF application that uses a DataGrid control.

I am going to try to describe a scenario that is happening in this applicaiton..

Here are the steps that produce the "error". I am trying to figure out what is going on:

The user adds a row to the DataGrid table. The user changes the first column (Qty) to 1. The user tabs across the columns. Once the cursor passes the "Discount Pct" column and enters the next column. At this momemt, the "Discount Pct" cell becomes outlined in Red. No other cells can be edited until the "Discount Pct" column has been "fixed". The funny thing is, you can clear the cell and type the same data and the red goes away.

I thought maybe this was because the field is a "percentage" field (when you type .10, it becomes "10 %" when you leave the cell), but the same thing is happening with another (plain text/varchar) column.

Other weird things I have noticed:

If the user doesn't enter anything in the Qty cell, but just tabs straight through all the columns, to the end, none of the cells turn red.

If the user clicks (instead of tab) on the Discount Pct cell (whether a Qty is entered or NOT), then leaves that cell (with a click or tab), the cell DOESN'T turn red.

Any ideas what is happening here? I suppose there is some type of Cell Validation failing, but why only under these specific circumstances?

How would you diagnose this? Can I display the validation error somehow (I am sure there must be a handler I can tie in to), maybe that will tell me something.

Thoughts? Ideas?

Adding code per requests:

        <DataGrid x:Name="myDataGrid" 
              ItemsSource="{Binding}" 
              CanUserAddRows="False" 
              CanUserDeleteRows="True" 
              CanUserResizeRows="False"
              SelectionUnit="CellOrRowHeader" 
              ColumnHeaderStyle="{StaticResource lclDataGridColumnHeaders}"
              RowHeaderStyle="{StaticResource lclDataGridRowHeaders}" 
              CellStyle="{StaticResource lclDataGridCellStyle}"
              HorizontalGridLinesBrush="LightBlue" 
              VerticalGridLinesBrush="LightBlue" 
              AutoGeneratingColumn="DataGrid_AutoGeneratingColumn" 
              Loaded="DataGrid_Loaded" 
              LoadingRow="DataGrid_LoadingRow"
              CurrentCellChanged="DataGrid_CurrentCellChanged"
              CellEditEnding="DataGrid_CellEditEnding">
    </DataGrid>

I figured out how to apply a Style to the "EditCell" and ued the following (from another site):

        <Style x:Key="lclDataGridCellEditStyle" TargetType="{x:Type TextBox}">
        <Setter Property="BorderThickness" Value="0"/>
        <Setter Property="Padding" Value="0"/>
        <Style.Triggers>
            <Trigger Property="Validation.HasError" Value="true">
                <Setter Property="ToolTip"
                        Value="{Binding RelativeSource={RelativeSource Self},
                                Path=(Validation.Errors)[0].ErrorContent}"/>
            </Trigger>
        </Style.Triggers>
    </Style>

Then, I assigned that to the EditingElementStyle on the datagrid (in code behind).

The Validation error generated (confirms my suspicions) is: "Value '10 %' could not be converted".

This makes sense, because it is wanting the user to enter ".10" or somethinig similar (a float/double/decimal/etc)...without the "%" (treating the entry as char/text).

The issue is, this row is "imported" (copied from another table) into the DataGrid and therefore, the field is already populated. So, how can I get the DataGrid to just accept the "display value" (throwing around terms and guessing here) for the "Edit Value"?

And, why only when you Change the Qty Cell and then tab through it, does this Validation error occur?

More on the other field later...once I "solve" this one, I suspect the solution will be similar...

I can give more code, but there is alot. I am trying to just give what is necessary. If I can post an of the other code (such as AutoGen开发者_高级运维eratingColumn, which is very basic), let me know!

New information... I am guessing the reason the red box shows up after the Qty is changed, is because that triggers a row change, which does a validation. And, to fix the percentage scenario I described above, it looks like I will need to implement a "converter" as described here: http://msdn.microsoft.com/en-us/library/system.windows.data.ivalueconverter.aspx

Another Update....

This worked. I added the Converter to the Percentage field and now all is good for that one.

The other field that is giving me problems is an "integer" column that allows nulls. When the record is imported, that column is null, but evidently the DataGridTextColumn wants ints to NOT be null. Is there a property I can set to allow nulls? Or, do I simply need to do another converter to convert between..um... null (string) and int?? lol

Shayne


You might be able to have a column that is of type int? which would allow for nulls.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号