none
Combobox的Background設定了圖片,但不會顯示 RRS feed

  • 问题

  • 請問各位,我在Combobox的Background裡設定的圖片,但是為什麼在控件不會顯示呢?

    還是我有哪裡搞錯,要怎樣做他才能顯示圖片?謝謝各位

    2017年11月16日 7:54

答案


  • Hi   魏高個 ,

    据我目前了解,在Windows 8和10中更改其背景您不能简单地设置ComboBox的Background属性。您需要定义自定义控件模板来实现:

    下面的文章讲解了如何修改ComboBox Style  和ComboBoxItemStyle 的模板。

    Changing The Background Color Of a ComboBox In WPF On Windows 8:
    https://social.technet.microsoft.com/wiki/contents/articles/24240.changing-the-background-color-of-a-combobox-in-wpf-on-windows-8.aspx

    我在下面做了一个Demo,来修改ComboBox Style (通过代码) 和ComboBoxItemStyle(通过模板) 的背景色。

    <Window.Resources>
            <Style x:Key="ComboBoxItemStyle1" TargetType="{x:Type ComboBoxItem}">
                <Setter Property="SnapsToDevicePixels" Value="True"/>
                <Setter Property="Padding" Value="4,1"/>
                <Setter Property="HorizontalContentAlignment" Value="{Binding HorizontalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
                <Setter Property="VerticalContentAlignment" Value="{Binding VerticalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
                <Setter Property="Background" Value="Yellow"/>
                <!-- Custom value -->
                <Setter Property="BorderBrush" Value="Yellow"/>
                <!-- Custom value -->
                <Setter Property="BorderThickness" Value="1"/>
            </Style>
        </Window.Resources>
    
    
     <ComboBox x:Name="combxoss" HorizontalAlignment="Left" Margin="328,49,0,0" Grid.Row="2" VerticalAlignment="Top" Width="169" Height="29"  
              ItemContainerStyle="{DynamicResource ComboBoxItemStyle1}"   Loaded="ComboBox_Loaded" >
                <ComboBoxItem Content="One"/>
                <ComboBoxItem Content="Two"/>
                <ComboBoxItem Content="Three"/>
            </ComboBox>

    XAML.CS:

      private void ComboBox_Loaded(object sender, RoutedEventArgs e)
            {
                var toggleButton = combxoss.Template.FindName("toggleButton", combxoss) as System.Windows.Controls.Primitives.ToggleButton;
                if (toggleButton != null)
                {
                    Border border = toggleButton.Template.FindName("templateRoot", toggleButton) as Border;
                    if (border != null)
                    {
                        border.Background = new ImageBrush(new BitmapImage(
        new Uri(@"C:\Users\Desktop\Wpf_temp\images\bac.JPG", UriKind.Relative)));
                    }
                    //border.Background = new ImageBrush();// Brushes.Transparent;
    
                }
    
            }


     
    Best Regards,

    Yong Lu



    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    2017年11月17日 4:05
    版主

全部回复

  • 你好

    直接修改 Background 确实是不生效的,需要重写 ComboBox 的 Style。

    因为 ComboBox 由两部分组成,默认显示的 ToggleButton,点击后划出的 ListView,这里需要重写的就是 ToggleButton 的样式。


    希望我的答案能帮助更多的人。
    博客地址: http://www.cnblogs.com/shaomeng

    2017年11月17日 3:03

  • Hi   魏高個 ,

    据我目前了解,在Windows 8和10中更改其背景您不能简单地设置ComboBox的Background属性。您需要定义自定义控件模板来实现:

    下面的文章讲解了如何修改ComboBox Style  和ComboBoxItemStyle 的模板。

    Changing The Background Color Of a ComboBox In WPF On Windows 8:
    https://social.technet.microsoft.com/wiki/contents/articles/24240.changing-the-background-color-of-a-combobox-in-wpf-on-windows-8.aspx

    我在下面做了一个Demo,来修改ComboBox Style (通过代码) 和ComboBoxItemStyle(通过模板) 的背景色。

    <Window.Resources>
            <Style x:Key="ComboBoxItemStyle1" TargetType="{x:Type ComboBoxItem}">
                <Setter Property="SnapsToDevicePixels" Value="True"/>
                <Setter Property="Padding" Value="4,1"/>
                <Setter Property="HorizontalContentAlignment" Value="{Binding HorizontalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
                <Setter Property="VerticalContentAlignment" Value="{Binding VerticalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
                <Setter Property="Background" Value="Yellow"/>
                <!-- Custom value -->
                <Setter Property="BorderBrush" Value="Yellow"/>
                <!-- Custom value -->
                <Setter Property="BorderThickness" Value="1"/>
            </Style>
        </Window.Resources>
    
    
     <ComboBox x:Name="combxoss" HorizontalAlignment="Left" Margin="328,49,0,0" Grid.Row="2" VerticalAlignment="Top" Width="169" Height="29"  
              ItemContainerStyle="{DynamicResource ComboBoxItemStyle1}"   Loaded="ComboBox_Loaded" >
                <ComboBoxItem Content="One"/>
                <ComboBoxItem Content="Two"/>
                <ComboBoxItem Content="Three"/>
            </ComboBox>

    XAML.CS:

      private void ComboBox_Loaded(object sender, RoutedEventArgs e)
            {
                var toggleButton = combxoss.Template.FindName("toggleButton", combxoss) as System.Windows.Controls.Primitives.ToggleButton;
                if (toggleButton != null)
                {
                    Border border = toggleButton.Template.FindName("templateRoot", toggleButton) as Border;
                    if (border != null)
                    {
                        border.Background = new ImageBrush(new BitmapImage(
        new Uri(@"C:\Users\Desktop\Wpf_temp\images\bac.JPG", UriKind.Relative)));
                    }
                    //border.Background = new ImageBrush();// Brushes.Transparent;
    
                }
    
            }


     
    Best Regards,

    Yong Lu



    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    2017年11月17日 4:05
    版主

  • Hi   魏高個 ,

    据我目前了解,在Windows 8和10中更改其背景您不能简单地设置ComboBox的Background属性。您需要定义自定义控件模板来实现:

    下面的文章讲解了如何修改ComboBox Style  和ComboBoxItemStyle 的模板。

    Changing The Background Color Of a ComboBox In WPF On Windows 8:
    https://social.technet.microsoft.com/wiki/contents/articles/24240.changing-the-background-color-of-a-combobox-in-wpf-on-windows-8.aspx

    我在下面做了一个Demo,来修改ComboBox Style (通过代码) 和ComboBoxItemStyle(通过模板) 的背景色。

    <Window.Resources>
            <Style x:Key="ComboBoxItemStyle1" TargetType="{x:Type ComboBoxItem}">
                <Setter Property="SnapsToDevicePixels" Value="True"/>
                <Setter Property="Padding" Value="4,1"/>
                <Setter Property="HorizontalContentAlignment" Value="{Binding HorizontalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
                <Setter Property="VerticalContentAlignment" Value="{Binding VerticalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
                <Setter Property="Background" Value="Yellow"/>
                <!-- Custom value -->
                <Setter Property="BorderBrush" Value="Yellow"/>
                <!-- Custom value -->
                <Setter Property="BorderThickness" Value="1"/>
            </Style>
        </Window.Resources>
    
    
     <ComboBox x:Name="combxoss" HorizontalAlignment="Left" Margin="328,49,0,0" Grid.Row="2" VerticalAlignment="Top" Width="169" Height="29"  
              ItemContainerStyle="{DynamicResource ComboBoxItemStyle1}"   Loaded="ComboBox_Loaded" >
                <ComboBoxItem Content="One"/>
                <ComboBoxItem Content="Two"/>
                <ComboBoxItem Content="Three"/>
            </ComboBox>

    XAML.CS:

      private void ComboBox_Loaded(object sender, RoutedEventArgs e)
            {
                var toggleButton = combxoss.Template.FindName("toggleButton", combxoss) as System.Windows.Controls.Primitives.ToggleButton;
                if (toggleButton != null)
                {
                    Border border = toggleButton.Template.FindName("templateRoot", toggleButton) as Border;
                    if (border != null)
                    {
                        border.Background = new ImageBrush(new BitmapImage(
        new Uri(@"C:\Users\Desktop\Wpf_temp\images\bac.JPG", UriKind.Relative)));
                    }
                    //border.Background = new ImageBrush();// Brushes.Transparent;
    
                }
    
            }


     
    Best Regards,

    Yong Lu



    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    謝謝你的解答!對我很有幫助
    2017年11月20日 3:45