Sean_Page
Sean_Page

Reputation: 25

ComboBox FontWeight binding in a CellTemplate of a DataGrid from the Main VM and not DG ItemSource

I have a DataGrid that is bound to a ObservableCollection of named Views (ViewItem). I have a CellTemplate column that I have a ComboBox inside a DockPanel that I am binding to an ObservableCollection on the Main VM named Sheets (SheetItem).

What I am trying to do is use a ValueConverter on the SheetItem to convert a bool (SheetIndex) to the FontWeight of the ComboBox item.

Here is the DataGrid:

<DataGrid ItemsSource="{Binding Views}" Grid.Row="1" Style="{StaticResource DefaultGrid}" SelectedItem="{Binding SelectedView,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" 
      d:ItemsSource="{d:SampleData}" AutoGenerateColumns="False">
<DataGrid.Columns>
    <DataGridTemplateColumn Header="View Name" Width="*" CanUserSort="True" SortMemberPath="Name">
        <DataGridTemplateColumn.CellTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding Name}" VerticalAlignment="Center" Margin="3 0"/>
            </DataTemplate>
        </DataGridTemplateColumn.CellTemplate>
    </DataGridTemplateColumn>
    <DataGridTemplateColumn Header="Sheet" Width="Auto" MinWidth="300">
        <DataGridTemplateColumn.CellTemplate>
            <DataTemplate>
                <DockPanel>
                    <Button Command="{Binding DataContext.ResetCMD, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}" Style="{StaticResource Round}" DockPanel.Dock="Right">
                        <Image Source="/Revit.Tools.25;component/Images/DeleteKeyedNotes.png" Height="20" Width="20"/>
                    </Button>
                    <ComboBox ItemsSource="{Binding DataContext.Sheets, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}" DisplayMemberPath="Identity" Margin="2" VerticalAlignment="Center" 
                              SelectedItem="{Binding SheetItem,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" VerticalContentAlignment="Center" FontWeight="{Binding SheetIndex, Converter={vc:FontWeightConverterBold}}" 
                              Foreground="{StaticResource RDGBlackBrush}" IsTextSearchEnabled="True" TextSearch.TextPath="Identity" IsTextSearchCaseSensitive="False"/>
                </DockPanel>
            </DataTemplate>
        </DataGridTemplateColumn.CellTemplate>
    </DataGridTemplateColumn>
</DataGrid.Columns>

Here is the ValueConverter:

public class FontWeightConverterBold : BaseValueConverter<FontWeightConverterBold>
{
    public override object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if((bool)value)
        {
            return FontWeights.Bold;
        }

        return FontWeights.Normal;
    }

    public override object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotSupportedException();
    }
}

I have checked (even manually setting value to true) that the property is being set, and I have used this converter in several other places, just not in this manner. Is there something simple I am overlooking, or is this just not how this should working?

Upvotes: 0

Views: 19

Answers (0)

Related Questions