You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

DataGrid单元格模板中ComboBox FontWeight绑定失效问题排查

问题排查:DataGrid中ComboBox的FontWeight绑定未生效

我有一个绑定到ViewItem类型ObservableCollection的DataGrid,其中某列CellTemplate内的ComboBox绑定到主ViewModel(Main VM)的SheetItem类型ObservableCollection(Sheets)。尝试用ValueConverter将SheetItem的bool类型属性SheetIndex转换为ComboBox的FontWeight,但绑定未生效。已确认SheetIndex属性值正确设置,且转换器在其他场景正常工作,想排查疏漏或用法错误。

XAML代码

<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>
</DataGrid>

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();
    }
}

排查要点

  • 绑定路径错误:当前ComboBox的FontWeight绑定{Binding SheetIndex}的上下文是ViewItem,但SheetIndex是SheetItem的属性,而SheetItem是ViewItem的子属性(从SelectedItem="{Binding SheetItem}"可确认)。正确绑定路径应为{Binding SheetItem.SheetIndex, Converter={vc:FontWeightConverterBold}}。
  • 转换器实例化验证:确认XAML中vc命名空间引用正确,且BaseValueConverter正确实现了MarkupExtension接口(因为使用了vc:FontWeightConverterBold标记扩展语法)。若未实现,需改为静态资源引用方式:先在资源中声明转换器,再用{StaticResource FontWeightConverterBold}。
  • 属性变更通知检查:确保SheetItem类实现了INotifyPropertyChanged接口,且SheetIndex属性值变化时触发了PropertyChanged事件,否则UI无法感知值更新。
  • 绑定日志调试:添加绑定跟踪日志定位问题,修改绑定代码:
    FontWeight="{Binding SheetItem.SheetIndex, Converter={vc:FontWeightConverterBold}, Diagnostics:PresentationTraceSources.TraceLevel=High}"
    
    需在XAML根节点添加命名空间:xmlns:Diagnostics="clr-namespace:System.Diagnostics;assembly=WindowsBase",然后查看输出窗口的绑定日志信息。

内容的提问来源于stack exchange,提问作者Sean_Page

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 14:55:59