Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 覆盖默认ComboxItem颜色_C#_Wpf_Xaml_Xamarin - Fatal编程技术网

C# 覆盖默认ComboxItem颜色

C# 覆盖默认ComboxItem颜色,c#,wpf,xaml,xamarin,C#,Wpf,Xaml,Xamarin,这可以覆盖Xamarin Forms WPF应用程序中的ComboBoxItem背景色,但不是很优雅,因为我必须在自定义渲染器中手动将模板应用于ComboxItems: 应用程序中的App.xaml。资源: <ControlTemplate TargetType="{x:Type ComboBoxItem}" x:Key="CustomComboBoxItem"> <Border BorderThickness=&

这可以覆盖Xamarin Forms WPF应用程序中的ComboBoxItem背景色,但不是很优雅,因为我必须在自定义渲染器中手动将模板应用于ComboxItems:

应用程序中的App.xaml。资源:

    <ControlTemplate TargetType="{x:Type ComboBoxItem}" x:Key="CustomComboBoxItem">
        <Border BorderThickness="{TemplateBinding Border.BorderThickness}" Padding="{TemplateBinding Control.Padding}" BorderBrush="{TemplateBinding Border.BorderBrush}" Background="{TemplateBinding Panel.Background}" Name="Bd" SnapsToDevicePixels="True">
            <ContentPresenter Content="{TemplateBinding ContentControl.Content}" ContentTemplate="{TemplateBinding ContentControl.ContentTemplate}" ContentStringFormat="{TemplateBinding ContentControl.ContentStringFormat}" HorizontalAlignment="{TemplateBinding Control.HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding Control.VerticalContentAlignment}" SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}" />
        </Border>
        <ControlTemplate.Triggers>
              ... All the regular triggers here override with new colors ...
        </ControlTemplate.Triggers>
    </ControlTemplate>

    <Style TargetType="{x:Type ComboBoxItem}">
        <Setter Property='OverridesDefaultStyle' Value='True'/>
        <Setter Property="Template" Value="{StaticResource CustomComboBoxItem}">
        </Setter>
    </Style>

... 这里所有的常规触发器都会覆盖新的颜色。。。
在自定义PickerRenderer中:

    protected override void UpdateNativeWidget()
    {
        base.UpdateNativeWidget();
        var c = Control;
        if (p == null)
        {
            template = System.Windows.Application.Current.Resources["CustomComboBoxItem"] as System.Windows.Controls.ControlTemplate;
            ItemsPresenter presenter = GetVisualChild<ItemsPresenter>(c);
            Popup p = VisualTreeHelper.GetChild(VisualTreeHelper.GetChild(c, 0), 0) as Popup;
            var a = (((((p.Child) as Decorator).Child as Border).Child as ScrollViewer).Content as System.Windows.Controls.Grid).Children;
            presenter = a[1] as ItemsPresenter;
            this.p = presenter;
            this.p.Loaded += Presenter_Loaded;
        }
    }

    ItemsPresenter p;
    ComboBoxItem[] items;
    System.Windows.Controls.ControlTemplate template;

    private void Presenter_Loaded(object sender, RoutedEventArgs e)
    {
        items = new ComboBoxItem[Control.ItemContainerGenerator.Items.Count];
        for(int i=0;i<items.Length;i++)
        {
            items[i] = Control.ItemContainerGenerator.ContainerFromIndex(i) as ComboBoxItem;
            if (template != null)
            {
                items[i].Template = template;
            }
        }
    }
protectedoverride void updatenactivewidget()的受保护覆盖
{
base.UpdateNativeWidget();
var c=对照组;
if(p==null)
{
template=System.Windows.Application.Current.Resources[“CustomComboBoxItem”]作为System.Windows.Controls.ControlTemplate;
ItemsPresenter=GetVisualChild(c);
弹出p=VisualTreeHelper.GetChild(VisualTreeHelper.GetChild(c,0),0)作为弹出窗口;
var a=(((((((p.Child)作为装饰程序)。Child作为边框)。Child作为滚动查看器)。内容作为系统。窗口。控件。网格)。Children;
presenter=a[1]作为项目重置中心;
this.p=演示者;
this.p.Loaded+=演示者_Loaded;
}
}
项目中心p;
ComboBoxItem[]项;
System.Windows.Controls.ControlTemplate模板;
已加载私有void演示者(对象发送者、路由目标)
{
items=newComboboxItem[Control.ItemContainerGenerator.items.Count];

如果希望将整个应用程序中的默认comBoxItem模板设置为customComboxItem,则为(int i=0;i)。 您只需将样式资源放在app.xaml中

<Application.Resources>
<Style TargetType="{x:Type ComboBoxItem}" x:Key="{x:Type ComboBoxItem}">
    <Setter Property='OverridesDefaultStyle' Value='True'/>
    <Setter Property="Template" Value="{StaticResource CustomComboBoxItem}">
    </Setter>
</Style>
</Application.Resources>

接受这个答案。我在PickerRenderer OnElementChanged中的代码中有一个位置,在那里使用了Contrtol.ItemContainerStyle,这就是导致应用程序.Resources样式无法应用的原因。
    <ControlTemplate TargetType="{x:Type ComboBoxItem}" x:Key="CustomComboBoxItem">
    </ControlTemplate>

    <Style TargetType="{x:Type ComboBoxItem}" x:Key="{x:Type ComboBoxItem}">
        <Setter Property='OverridesDefaultStyle' Value='True'/>
        <Setter Property="Template" Value="{StaticResource CustomComboBoxItem}">
        </Setter>
    </Style>
<Application.Resources>
<Style TargetType="{x:Type ComboBoxItem}" x:Key="{x:Type ComboBoxItem}">
    <Setter Property='OverridesDefaultStyle' Value='True'/>
    <Setter Property="Template" Value="{StaticResource CustomComboBoxItem}">
    </Setter>
</Style>
</Application.Resources>