Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
180
Set Background color of one field/column in xamDataGrid only if it is editable
posted

Trying to find a way to set the background color when a field is editable. If I just set the AllowEdit property to true/false then I can do what I want.

However, I need to bind the AllowEdit property to a property in my view model. Not matter what I've tried nothing seems to work.

I'm sure there is a simple solution to this but nothing I've done works.

My xaml looks something like this but Field2 obviously doens't work

        <igDp:XamDataGrid >
            <igDp:XamDataGrid.FieldSettings>
                <igDp:FieldSettings CellClickAction="SelectRecord" LabelClickAction="Nothing" AllowSummaries="False"/>
            </igDp:XamDataGrid.FieldSettings>
            <igDp:XamDataGrid.FieldLayoutSettings>
                <igDp:FieldLayoutSettings AutoGenerateFields="False" RecordSelectorLocation="None" AllowAddNew="False" AllowDelete="False" AllowFieldMoving="No"/>
            </igDp:XamDataGrid.FieldLayoutSettings>

            <igDp:XamDataGrid.FieldLayouts>
                <igDp:FieldLayout>
                    <igDp:FieldLayout.SummaryDefinitions>
                        <igDp:SummaryDefinition Key="thickness_sum" SourceFieldName="Thickness" Calculator="Sum" DisplayArea="BottomFixed"/>
                    </igDp:FieldLayout.SummaryDefinitions>
                    <igDp:FieldLayout.Fields>
                        <igDp:TextField Name="Field1" Label="Field1" AllowEdit="False" />

<-- I know the code below won't work but I think this lets you see what I'm trying to do. -->
                        <igDp:NumericField Name="Field2" Label="Field2" AllowEdit="{Binding IsEditable}">
                            <igDp:NumericField.CellValuePresenterStyle>
                                <Style TargetType="{x:Type igDp:CellValuePresenter}">
                                    <Style.Triggers>
                                        <Trigger Property="AllowEdit" Value="True">
                                            <Setter Property="Background" Value="Beige"></Setter>
                                        </Trigger>
                                        <Trigger Property="AllowEdit" Value="False">
                                            <Setter Property="Background" Value="White"></Setter>
                                        </Trigger>
                                    </Style.Triggers>
                                </Style>
                            </igDp:NumericField.CellValuePresenterStyle>
                        </igDp:NumericField>
                        
                        <igDp:TextField Name="Field3" Label="Field2" AllowEdit="False" />
                        <igDp:TextField Name="Field4" Label="Field2" AllowEdit="False" />

                    </igDp:FieldLayout.Fields>
                </igDp:FieldLayout>
            </igDp:XamDataGrid.FieldLayouts>
        </igDp:XamDataGrid>

Parents
  • 34430
    Verified Answer
    Offline posted

    Hello Michael,

    Binding directly to a ViewModel from a Field object is not possible in the normal binding way because the Field objects are not a visual element and so do not expose a DataContext inherited from FrameworkElement, which bindings use.

    Instead, I would recommend that you see this documentation article about FieldBindings, which we exposed so that you can bind to ViewModel properties from Fields.

    Regarding your Style, this also will not work, as there is no AllowEdit property on the CellValuePresenter element. Instead of using your Triggers, I would recommend using DataTriggers in this case, which could look like the following:

    <DataTrigger Binding=”{Binding RelativeSource={RelativeSource Self}, Path=Field.AllowEdit}” Value=”True”>

    Please let me know if you have any other questions or concerns on this matter.

Reply Children
No Data