【问题标题】:Unable to change property无法更改属性
【发布时间】:2013-03-08 15:57:33
【问题描述】:

我正在尝试更改 ComboBox 的边框 - 一项看似简单的任务,但没有发生任何事情。

编辑:

这是我的代码:

以下内容出现在事件中:

cmb.BorderThickness = new Thickness(3);
cmb.BorderBrush = Brushes.Green;

这是 XAML:

    <UserControl x:Class="QueryBuilder.FilterWindow"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" MaxHeight="200" Background="White" PreviewMouseLeftButtonDown="UserControl_PreviewMouseLeftButtonDown">
    <UserControl.Resources>
        <Style TargetType="TextBox">
            <Setter Property="VerticalAlignment" Value="Center"/>
            <Setter Property="BorderBrush" Value="Silver"/>
            <Setter Property="BorderThickness" Value="1"/>
            <Setter Property="SnapsToDevicePixels" Value="True"/>
            <Setter Property="OverridesDefaultStyle" Value="True"/>
            <Setter Property="FocusVisualStyle" Value="{x:Null}"/>
            <Setter Property="AllowDrop" Value="true"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type TextBoxBase}">
                        <Border Name="Border" Padding="1" Background="#FFFFFF" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" >
                            <ScrollViewer Margin="0" x:Name="PART_ContentHost"/>
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsEnabled" Value="False">
                                <Setter TargetName="Border" Property="Background" Value="#EEEEEE"/>
                                <Setter TargetName="Border" Property="BorderBrush" Value="#EEEEEE"/>
                                <Setter Property="Foreground" Value="#888888"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
        <Style TargetType="TreeViewItem">
            <Setter Property="Template" Value="{StaticResource TreeViewWithLines}"/>
            <Setter Property="Padding" Value="0,5,0,5"/>
            <Setter Property="IsExpanded" Value="true"/>
        </Style>
    </UserControl.Resources>
    <ScrollViewer ScrollViewer.HorizontalScrollBarVisibility="Auto" ScrollViewer.VerticalScrollBarVisibility="Auto">
        <TreeView Name="tvFilters" Margin="0,10,0,10" Background="White" BorderBrush="White"/>
    </ScrollViewer>
</UserControl>      

我使用 ComboBox 的默认控件模板,所以我不知道为什么它不起作用。有什么想法吗?

【问题讨论】:

  • 不要在代码中操作 UI 元素。
  • 你设置BorderThickness了吗?
  • 您使用的是 Windows 8 吗?我似乎记得在 Windows 8 上呈现其他控件(例如 ToggleButton)的方式存在一些错误。我刚刚尝试了 Dom 在我的 Windows 8 机器上发布的解决方案,而 BorderThickness 更改了 @987654326 @ 没有。
  • @Benjamin - 我实际上使用的是 Windows 8。你会推荐什么?我已经让它适用于文本框,但不适用于组合框
  • kmatyaszek 答案可能是此时您唯一可行的选择。定义一个新的控件模板。

标签: c# .net wpf user-interface mvvm


【解决方案1】:

当我想修改样式时,我会使用 WPF Inspector(你可以在那里找到它:http://wpfinspector.codeplex.com/)

然后附加到您的 ComboBox,您就可以访问所有属性和样式...您可以实时修改它们并查看结果,速度很快....

也许你的边框是空的?还是厚度为0?

【讨论】:

    【解决方案2】:

    您应该定义新的 ControlTemplate,here 您拥有所有代码。

    如果您想更改ComboBox 的边框,您应该从msdn 复制并粘贴代码并仅更改以下样式:

    <Style x:Key="{x:Type ComboBox}" TargetType="ComboBox">
        <Setter Property="SnapsToDevicePixels" Value="true"/>
        <Setter Property="OverridesDefaultStyle" Value="true"/>
        <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto"/>
        <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto"/>
        <Setter Property="ScrollViewer.CanContentScroll" Value="true"/>
        <Setter Property="MinWidth" Value="120"/>
        <Setter Property="MinHeight" Value="20"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ComboBox">
                    <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}">
                        <Grid>
                            <ToggleButton 
                            Name="ToggleButton" 
                            Template="{StaticResource ComboBoxToggleButton}" 
                            Grid.Column="2" 
                            Focusable="false"
                            IsChecked="{Binding Path=IsDropDownOpen,Mode=TwoWay,RelativeSource={RelativeSource TemplatedParent}}"
                            ClickMode="Press">
                            </ToggleButton>
                            <ContentPresenter
                            Name="ContentSite"
                            IsHitTestVisible="False" 
                            Content="{TemplateBinding SelectionBoxItem}"
                            ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}"
                            ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}"
                            Margin="3,3,23,3"
                            VerticalAlignment="Center"
                            HorizontalAlignment="Left" />
                            <TextBox x:Name="PART_EditableTextBox"
                                 Style="{x:Null}" 
                                 Template="{StaticResource ComboBoxTextBox}" 
                                 HorizontalAlignment="Left" 
                                 VerticalAlignment="Center" 
                                 Margin="3,3,23,3"
                                 Focusable="True" 
                                 Background="Transparent"
                                 Visibility="Hidden"
                                 IsReadOnly="{TemplateBinding IsReadOnly}"/>
                            <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"
                                    Background="{StaticResource WindowBackgroundBrush}"
                                    BorderThickness="1"
                                    BorderBrush="{StaticResource SolidBorderBrush}"/>
                                    <ScrollViewer Margin="4,6,4,6" SnapsToDevicePixels="True">
                                        <StackPanel IsItemsHost="True" KeyboardNavigation.DirectionalNavigation="Contained" />
                                    </ScrollViewer>
                                </Grid>
                            </Popup>
                        </Grid>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="HasItems" Value="false">
                            <Setter TargetName="DropDownBorder" Property="MinHeight" Value="95"/>
                        </Trigger>
                        <Trigger Property="IsEnabled" Value="false">
                            <Setter Property="Foreground" Value="{StaticResource DisabledForegroundBrush}"/>
                        </Trigger>
                        <Trigger Property="IsGrouping" Value="true">
                            <Setter Property="ScrollViewer.CanContentScroll" Value="false"/>
                        </Trigger>
                        <Trigger SourceName="Popup" Property="Popup.AllowsTransparency" Value="true">
                            <Setter TargetName="DropDownBorder" Property="CornerRadius" Value="4"/>
                            <Setter TargetName="DropDownBorder" Property="Margin" Value="0,2,0,0"/>
                        </Trigger>
                        <Trigger Property="IsEditable"
                                 Value="true">
                            <Setter Property="IsTabStop" Value="false"/>
                            <Setter TargetName="PART_EditableTextBox" Property="Visibility"    Value="Visible"/>
                            <Setter TargetName="ContentSite" Property="Visibility" Value="Hidden"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Style.Triggers>
        </Style.Triggers>
    </Style>
    

    使用示例:

    XAML:

    <ComboBox Height="30" Width="150" BorderBrush="Red" BorderThickness="6" />
    

    结果:

    XAML:

    <ComboBox Height="30" Width="150" BorderBrush="Green" BorderThickness="3" />
    

    结果:

    【讨论】:

    • 我不是投反对票的人。尚未尝试您的方法,但我编辑了问题
    • 嗯,好的,所以@DotNET 你应该试试,我测试了它,它正在工作:)
    • 我很快就尝试了,但它改变了 ComboBox 的外观和感觉太多。它现在看起来太奇怪了:/而且,似乎仍然无法正常工作
    • @DotNET 我为你准备了示例解决方案,你可以从这个链接下载它:sdrv.ms/ZsH1NG。如果现在为您工作,请告诉我。嗯..我认为这是更改它的唯一方法,当您尝试更改默认样式时,您无法更改BorderThickness,因此您应该定义新的控件模板。
    • 我的要求是动态改变颜色和厚度。尝试从示例项目中的代码执行此操作时,没有任何反应
    猜你喜欢
    • 2019-01-31
    • 1970-01-01
    • 2022-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多