Create a MultiValueConverter for a DataGrid textbox using DataTrigger MultiBinding

I have one DataGrid, there is Combobox in Datagrid First column, need to pass ComboBox selection value "Type" and ItemSource of DataGrid column Combobox ItemSource DataContext.SubTestCollection to MultiBinding DataTrigger.and based on MultiConverter TextBlock Foreground color change

Code:

    <DataGrid x:Name="dtGrid" Grid.Row="1" Background="Transparent" ItemsSource="{Binding Path=LabTests, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" 
              IsSynchronizedWithCurrentItem="True" AutoGenerateColumns="False" SelectionUnit="Cell"
              CanUserAddRows="True" CanUserResizeColumns="True" CellEditEnding="dtGrid_CellEditEnding"
              AreRowDetailsFrozen="True" GridLinesVisibility="Horizontal" Height="150"
              InitializingNewItem="dtGrid_InitializingNewItem" DataGridCell.Selected="DataGrid_GotFocus"
              RowEditEnding="dtGrid_RowEditEnding">
        <DataGrid.Columns>
            <DataGridTemplateColumn Header="Name" HeaderStyle="{StaticResource DatagridColumnHeaderStyle}" Width="220">
                <DataGridTemplateColumn.CellEditingTemplate>
                    <DataTemplate>
                        <control:AutoComboBox x:Name="cbTestType"
                                 ItemsSource="{Binding DataContext.SubTestCollection, RelativeSource={RelativeSource AncestorType=local:AddLabTest}}"
                                 SelectedValue="{Binding Path=Type,UpdateSourceTrigger=PropertyChanged}"
                                 DisplayMemberPath="Name" SelectedValuePath="Name" IsEditable="True"
                                 Loaded="cbTestType_Loaded">
                            <control:AutoComboBox.DropDownView>
                                <GridView>
                                    <GridViewColumn Header="Name" DisplayMemberBinding="{Binding Path=Name}" />
                                </GridView>
                            </control:AutoComboBox.DropDownView>
                        </control:AutoComboBox>
                    </DataTemplate>
                </DataGridTemplateColumn.CellEditingTemplate>
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <TextBlock IsHitTestVisible="False" Text="{Binding Path=Type}"/>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
            <DataGridTemplateColumn Header="Result" HeaderStyle="{StaticResource DatagridColumnHeaderStyle}" Width="220">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <TextBox Text="{Binding Result,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}">
                            <TextBox.Style>
                                <Style TargetType="TextBox">
                                    <Style.Triggers>
                                        <DataTrigger Value="True">
                                            <DataTrigger.Binding>
                                                <MultiBinding Converter="{StaticResource RangeToBoolConverter}">
                                                    <Binding Path="123"></Binding>
                                                    <Binding ElementName="cbTestType" Path="Type"></Binding>
                                                </MultiBinding>
                                            </DataTrigger.Binding>
                                            <Setter Property="TextBox.Foreground" Value="Green" />
                                        </DataTrigger>
                                        <!--<DataTrigger Binding="{Binding Path=Type,Converter={StaticResource RangeToBoolConverter}}"
                                                     Value="True">
                                            <Setter Property="FontWeight" Value="SemiBold"/>
                                        </DataTrigger>-->
                                    </Style.Triggers>
                                </Style>
                            </TextBox.Style>
                        </TextBox>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
            <!--<DataGridTextColumn Header="Result" HeaderStyle="{StaticResource DatagridColumnHeaderStyle}" Width="100"
                                Binding="{Binding Result, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>-->
            <!--<DataGridTextColumn Header="Normal Range" HeaderStyle="{StaticResource DatagridColumnHeaderStyle}" Width="170"
                                Binding="{Binding NormalRange, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>-->
            <DataGridTemplateColumn Header="Normal Range" HeaderStyle="{StaticResource DatagridColumnHeaderStyle}"
                                    Width="170">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <TextBlock IsHitTestVisible="False"
                                   Text="{Binding Path=NormalRange, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
                <DataGridTemplateColumn.CellEditingTemplate>
                    <DataTemplate>
                        <ContentControl Content="{Binding}">
                            <ContentControl.Style>
                                <Style TargetType="ContentControl">
                                    <Setter Property="ContentTemplate">
                                        <Setter.Value>
                                            <DataTemplate>
                                                <TextBox Text="{Binding NormalRange, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
                                            </DataTemplate>
                                        </Setter.Value>
                                    </Setter>
                                    <Style.Triggers>
                                        <DataTrigger Binding="{Binding Parameter}" Value="IsPostiveType">
                                            <Setter Property="ContentTemplate">
                                                <Setter.Value>
                                                    <DataTemplate>
                                                        <control:AutoComboBox
                                                                              ItemsSource="{Binding DataContext.RangeFlags, RelativeSource={RelativeSource AncestorType=local:AddLabTest}}"
                                                                              SelectedValue="{Binding Path=NormalRange,UpdateSourceTrigger=PropertyChanged}"
                                                                              >
                                                            <control:AutoComboBox.DropDownView>
                                                                <GridView>
                                                                    <GridViewColumn Header="Name" DisplayMemberBinding="{Binding Path=.}" />
                                                                </GridView>
                                                            </control:AutoComboBox.DropDownView>
                                                        </control:AutoComboBox>
                                                    </DataTemplate>
                                                </Setter.Value>
                                            </Setter>
                                        </DataTrigger>
                                        <DataTrigger Binding="{Binding Parameter}" Value="IsNotPositiveType">
                                            <Setter Property="ContentTemplate">
                                                <Setter.Value>
                                                    <DataTemplate>
                                                        <TextBox Text="{Binding NormalRange, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" 
                                                                 FocusManager.FocusedElement="{Binding RelativeSource={RelativeSource Self}}"/>
                                                    </DataTemplate>
                                                </Setter.Value>
                                            </Setter>
                                        </DataTrigger>
                                    </Style.Triggers>
                                </Style>
                            </ContentControl.Style>
                        </ContentControl>
                    </DataTemplate>
                </DataGridTemplateColumn.CellEditingTemplate>
            </DataGridTemplateColumn>
            <DataGridTextColumn Header="Unit" HeaderStyle="{StaticResource DatagridColumnHeaderStyle}" Width="80"
                                Binding="{Binding Unit, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" 
                                FocusManager.FocusedElement="{Binding RelativeSource={RelativeSource Self}}"/>
            <DataGridTemplateColumn CanUserSort="False" HeaderStyle="{StaticResource DatagridColumnHeaderStyle}">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <control:IconButton x:Name="btnremove"  VectorIcon="{StaticResource CloseIconPath}" Width="15" 
                            Height="15" ToolTip="Remove Content" Foreground="Red" Cursor="Hand"
                            Command="{Binding ElementName=dtGrid, Path=DataContext.RemoveContentCommand}"
                            CommandParameter="{Binding ElementName=btnremove,Path=DataContext}"/>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
        </DataGrid.Columns>
    </DataGrid>

      

RangeToBoolConverter:

public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture) {
            var NormalRange = values[0] as LabParamItem;
            //var v = values[1].ToString();
            return values;
        }

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {
            string NormalRange = value as string;
            return NormalRange;
        } } }

      

I've tried so many things, searched on google and SO.but couldn't help me. any help would be appreciated.

+3


source to share





All Articles