WPF 组合框样式不绑定属性

问题描述

在我的 WPF 项目中,我有这种 import pandas as pd # create the initial dataframe from api_results df = pd.json_normalize(api_results).explode('icdcodes').reset_index(drop=True) # create a dataframe for only icdcodes,which will expand all the lists of dicts icdcodes = pd.json_normalize(df.icdcodes) # join df to icdcodes and drop the icdcodes column df = df.join(icdcodes).drop(['icdcodes'],axis=1) # display(df) requesturl clientid adjustedsummaryguidelines.midrangeallabsence adjustedsummaryguidelines.midrangeclaims adjustedsummaryguidelines.atriskallabsence adjustedsummaryguidelines.atriskclaims riskassessment.score riskassessment.status riskassessment.magnitude riskassessment.volatility adjustedduration.bp.days adjustedduration.cp95.alert adjustedduration.cp95.days adjustedduration.cp100.alert adjustedduration.cp100.days cfactors.legalrep.applied cfactors.legalrep.alert alertdesc.red alertdesc.yellow code name meandurationdays.bp meandurationdays.cp95 meandurationdays.cp100 0 http:\/\/www.odg-twc.com\/index.html?calculator.htm?icd=840.9~719.41&age=-1&state=IL&jobclass=1&cf=4 123456789 46 36 374 98 87.95 Red (Extreme) 86.65 89.25 2 yellow 185 yellow 365 1 red Recommend early intervention and priority medical case management. Consider early intervention and priority medical case management. 719.41 Pain in joint,shoulder region 18 72 93 1 http:\/\/www.odg-twc.com\/index.html?calculator.htm?icd=840.9~719.41&age=-1&state=IL&jobclass=1&cf=4 123456789 46 36 374 98 87.95 Red (Extreme) 86.65 89.25 2 yellow 185 yellow 365 1 red Recommend early intervention and priority medical case management. Consider early intervention and priority medical case management. 840.9 Sprains and strains of unspecified site of shoulder and upper arm 10 27 35 2 NaN 987654321 25 42 0 194 76.85 Orange (High) 74.44 79.25 2 NaN 95 yellow 193 1 red Recommend early intervention and priority medical case management. Consider early intervention and priority medical case management. 724.2 Lumbago 10 38 50 3 NaN 987654321 25 42 0 194 76.85 Orange (High) 74.44 79.25 2 NaN 95 yellow 193 1 red Recommend early intervention and priority medical case management. Consider early intervention and priority medical case management. 847.2 Sprain of lumbar 10 22 29 样式:

ComboBox

但是在箭头下拉部分(样式为边框)上,我尝试将 <Style x:Key="ComboBoxTextBoxStyle" targettype="{x:Type TextBox}"> <Setter Property="Template"> <Setter.Value> <ControlTemplate targettype="{x:Type TextBox}"> <Grid> <Border CornerRadius="5,5" Background="{TemplateBinding Background}" > <ScrollViewer x:Name="PART_ContentHost"/> </Border> </Grid> </ControlTemplate> </Setter.Value> </Setter> </Style> <Style x:Key="comboBoxitem" targettype="ComboBoxItem"> <Setter Property="Template"> <Setter.Value> <ControlTemplate targettype="ComboBoxItem"> <Border CornerRadius="3" Height="30" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}"> <ContentPresenter VerticalAlignment="Center"/> </Border> <ControlTemplate.Triggers> <Trigger Property="IsMouSEOver" Value="True"> <Setter Property="Background" > <Setter.Value> <SolidColorBrush Color="{DynamicResource BackgroundColour}"/> </Setter.Value> </Setter> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style> <Style x:Key="comboBox" targettype="{x:Type ComboBox}"> <Setter Property="Template"> <Setter.Value> <ControlTemplate targettype="{x:Type ComboBox}"> <Grid> <Grid.ColumnDeFinitions> <ColumnDeFinition/> <ColumnDeFinition MaxWidth="18"/> </Grid.ColumnDeFinitions> <TextBox Background="{TemplateBinding Background}" Name="PART_EditableTextBox" IsReadOnly="{TemplateBinding IsReadOnly}" FontSize="{TemplateBinding FontSize}" Style="{StaticResource ComboBoxTextBoxStyle}" Foreground="White" VerticalAlignment="Center" Height="{TemplateBinding Height}"/> <ToggleButton Grid.Column="1" Background="{TemplateBinding Background}" Margin="0" Height="{TemplateBinding Height}" Focusable="False" Foreground="White" IsChecked="{Binding Path=IsDropDownopen,Mode=TwoWay,RelativeSource={RelativeSource TemplatedParent}}" ClickMode="Press"> <ToggleButton.Template> <ControlTemplate> <Border BorderBrush="{TemplateBinding BorderBrush}" CornerRadius="0 5 5 0"> <Border.Background> <SolidColorBrush Color="{TemplateBinding Background}"/> </Border.Background> <Path Grid.Column="1" HorizontalAlignment="Center" VerticalAlignment="Center" Data="M 0 0 L 4 4 L 8 0 Z" Fill="#DF464B" /> </Border> </ControlTemplate> </ToggleButton.Template> </ToggleButton> <ContentPresenter Name="ContentSite" Content="{TemplateBinding SelectionBoxItem}" ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}" ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}" VerticalAlignment="Center" HorizontalAlignment="Left" Margin="5,0"/> <Popup Name="Popup" Placement="Bottom" IsOpen="{TemplateBinding IsDropDownopen}" AllowsTransparency="True" Focusable="False" PopupAnimation="Slide"> <Grid Name="DropDown" SnapsToDevicePixels="True" MinWidth="{TemplateBinding ActualWidth}" MaxHeight="{TemplateBinding MaxDropDownHeight}"> <Border x:Name="DropDownBorder" BorderThickness="1" CornerRadius="0 0 5 5" Background="{TemplateBinding Background}" BorderBrush="#DF464B"/> <ScrollViewer Margin="4,6,4,6" SnapsToDevicePixels="True"> <StackPanel IsItemsHost="True" KeyboardNavigation.DirectionalNavigation="Contained"> </StackPanel> </ScrollViewer> </Grid> </Popup> </Grid> </ControlTemplate> </Setter.Value> </Setter> </Style> 属性设置为使用 Background 绑定到模板的属性,但它不起作用。我猜这与它试图从实际边框获取 <SolidColorBrush Color="{TemplateBinding Background}"/> 而不是当我像这样调用组合框时获取 Background 的事实有关:<ComboBox Style="comboBox" Background="DesiredBackground"/>。我怎样才能使它绑定到正确的背景?

解决方法

您应该将边框的背景属性而不是颜色绑定到组合框背景属性。

它也适用于“TemplateBinding”,但是您绑定的是 ToggleButton 而不是 ComboBox。但由于 ToggleButton 是从 ComboBox 获取其背景,因此无论如何它都可以工作。

   <Border BorderBrush="{TemplateBinding BorderBrush}" CornerRadius="0 5 5 0"
           Background="{Binding Background,RelativeSource={RelativeSource FindAncestor,AncestorType=ComboBox}}">