Home > Back-end >  WPF : binding DataGrid outside the list
WPF : binding DataGrid outside the list

Time:06-28

<Grid Background="white" Margin="0,0,0,0" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">

    <Label Content="Physical Test" Margin="0,0,0,0" FontSize="12" FontWeight="DemiBold"
           HorizontalAlignment="Left"  VerticalAlignment="Top" />
    <ComboBox Height="22" Margin="90,0,0,0"
              HorizontalAlignment="Left"  VerticalAlignment="Top" 
              ItemsSource="{Binding physicalTestList, Mode=OneWay}" SelectedItem="{Binding selectedPhysicalTest}"
              DisplayMemberPath="physicalTestName" SelectedValuePath="physicalTestID"/>


    <Button Content="Refresh" 
            HorizontalAlignment="Left"  VerticalAlignment="Top" 
            Margin="200,0,0,0" Width="70" Height="22"
            Command="{Binding buttonRefresh}"    IsEnabled="{Binding RefreshEnable, Mode=OneWay}"/>
     
    <CheckBox Margin="300,5,0,0" HorizontalAlignment="Left"
              Content="Confirmed?"
              IsChecked="{Binding IsConfirmed, Mode=OneWay}"
              Command="{Binding buttonConfirm}" IsEnabled="{Binding ConfirmEnable, Mode=OneWay}"
              />
    

    <Grid>  
        <DataGrid  x:Name="theGrid" IsVisibleChanged="OnIsVisibleChanged"
                   Margin="0,30,0,0" HorizontalAlignment="Stretch"  VerticalAlignment="Stretch" 
                   AutoGenerateColumns="False"  CanUserAddRows="False"  IsReadOnly="False"  
                   ItemsSource="{Binding theList, Mode=OneWay}"
                   >
            <DataGrid.Resources>
                <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Navy"/>
                <SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey}" Color="SteelBlue"/>
            </DataGrid.Resources>
            <DataGrid.Columns>
                <DataGridTextColumn Header="Well"  Binding="{Binding well}" IsReadOnly="True" />
                <DataGridTextColumn Header="Control"  Binding="{Binding controlName}"  IsReadOnly="True" />
                <DataGridTextColumn Header="Type"  Binding="{Binding display}"  IsReadOnly="True" />
                <DataGridTemplateColumn Header="Value" IsReadOnly="True" >
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <Label Content="{Binding processedValue }" 
                                   Background="{Binding valueColor}"  />
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
                <DataGridTextColumn Header="Min"  Binding="{Binding minDisplay }"  IsReadOnly="True" />
                <DataGridTextColumn Header="Max"  Binding="{Binding maxDisplay }"  IsReadOnly="True" />
                <DataGridCheckBoxColumn Header="Omit For SPC?" Binding="{Binding isOutlier}" IsReadOnly="True" />

            </DataGrid.Columns>
        </DataGrid>

        

    </Grid>

</Grid>

I have Checkbox object's property IsChecked properly, using the following

IsChecked="{Binding IsConfirmed, Mode=OneWay}"

In the DataGrid "theGrid", I want to make the last column to have its ReadOnly property to TRUE if IsConfirmed = true. So basically I want the column to behave just like the checkbox outside the DataGrid.

At this point I have

<DataGridCheckBoxColumn Header="Omit For SPC?" Binding="{Binding isOutlier}" IsReadOnly="True" />

I tried to change it to

<DataGridCheckBoxColumn Header="Omit For SPC?" Binding="{Binding isOutlier}" IsReadOnly="{Binding IsConfirmed, Mode=OneWay}" />

but didn't work.

Any pointers would be greatly appreciated!

CodePudding user response:

You should use RelativeSource in your binding in this case.

For example:

<DataGridCheckBoxColumn Header="Omit For SPC?" Binding="{Binding isOutlier}" IsReadOnly="{Binding IsConfirmed, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type typeOfAncestor}} Mode=OneWay}" />

where typeOfAncestor is type of control who has IsConfirmed property in its DataContext model.

CodePudding user response:

it didn't work. This is what I have now.

<DataGridCheckBoxColumn Header="Omit For SPC?" Binding="{Binding isOutlier}" 
                        IsReadOnly="{Binding    IsConfirmed
                                                , RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type CheckBox}}
                                                , Mode=OneWay
                                                }" />

CodePudding user response:

Fixed.

You have to add binding object

<DataGrid>
            <DataGrid.Resources>
                <util:BindingProxy x:Key="proxy" Data="{Binding}" />
            </DataGrid.Resources> 

Then this is how you user that "proxy" reference

 <DataGridCheckBoxColumn Header="Omit For SPC?" Binding="{Binding isOutlier}" 
             IsReadOnly="{Binding Data.IsConfirmed, Source={StaticResource proxy} }" />

Note that you have to use "Data." instead of "DataContext." Then you need to create that "<util:" prefix

xmlns:util="clr-namespace:PCRPlating.util"

The following is class "BindingProxy"

    public class BindingProxy : Freezable
{
    #region Overrides of Freezable

    protected override Freezable CreateInstanceCore()
    {
        return new BindingProxy();
    }

    #endregion

    public object Data
    {
        get { return (object)GetValue(DataProperty); }
        set { SetValue(DataProperty, value); }
    }

    // Using a DependencyProperty as the backing store for Data.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty DataProperty =
        DependencyProperty.Register("Data", typeof(object), typeof(BindingProxy), new UIPropertyMetadata(null));
}
  • Related