2016-05-03 7 views
2

私はこれにいくつかの助けが必要です。Borderbrush to ComboBox

私はComboBoxとTextBoxの2つのコントロールを持っていますが、BorderBrushプロパティを他の色に変更する必要があります。TextBoxは完全に機能しますが、ComboBoxはありません。これは私のコードです。

<ComboBox x:Name="cmbCombo" HorizontalAlignment="Left" Margin="155,98,0,0" VerticalAlignment="Top" Width="120" />     
<TextBox x:Name="txtCaja" HorizontalAlignment="Left" Height="23" Margin="158,56,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120"/> 
<Button Content="Button" HorizontalAlignment="Left" Margin="174,155,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click_1"/> 


cmbCombo.BorderThickness = new Thickness(3); 
cmbCombo.BorderBrush = Brushes.OrangeRed; 
txtCaja.BorderThickness = new Thickness(3); 
txtCaja.BorderBrush = Brushes.OrangeRed; 

ありがとうございます。

+0

を "動作しない" の定義? [白い境界](http://stackoverflow.com/q/31524197/1997232)? [this]の欺瞞かもしれない(http://stackoverflow.com/q/10908678/1997232)。 – Sinatr

答えて

2

所望の色やブラシを作るためにプロパティBorderBrushを設定することが可能である。

<ComboBox Margin="10" BorderBrush="Red"> 
    <ComboBoxItem>No Device Selected</ComboBoxItem> 
</ComboBox> 

結果:

Screenshot of combo box with red border

あなたは太い枠線を持つようにしたい場合は、BorderThicknessプロパティを使用します。

<ComboBox Margin="10" BorderBrush="Red" BorderThickness="3"> 
    <ComboBoxItem>No Device Selected</ComboBoxItem> 
</ComboBox> 

を更新:あなたのComboBoxControltemplateに書くべきWindows 8については

BasedOn="{StaticResource {x:Type ComboBox}}" 
例えば

<Style x:Key="ComboBoxStyle1" TargetType="{x:Type ComboBox}" BasedOn="{StaticResource {x:Type 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="TextElement.Foreground" Value="Black"/> 
<Setter Property="FocusVisualStyle" Value="{x:Null}"/> 
<Setter Property="Template"> 
    <Setter.Value> 
     <ControlTemplate TargetType="{x:Type ComboBox}"> 
      <Border BorderBrush="{TemplateBinding BorderBrush}" 
     BorderThickness="{TemplateBinding BorderThickness}"> 
      <Grid> 
       <ToggleButton x:Name="ToggleButton" Grid.Column="2" 
        ClickMode="Press" Focusable="False" 
        IsChecked="{Binding IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}" 
        Template="{StaticResource ComboBoxToggleButtonTemplate}"/> 

       <ContentPresenter x:Name="ContentSite" Margin="5, 3, 23, 3" IsHitTestVisible="False" 
        HorizontalAlignment="Left" VerticalAlignment="Center"        
        Content="{TemplateBinding SelectionBoxItem}" 
        ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}" 
        ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}"/> 

       <TextBox x:Name="PART_EditableTextBox" Margin="3, 3, 23, 3"      
        IsReadOnly="{TemplateBinding IsReadOnly}" 
        Visibility="Hidden" Background="Transparent" 
        HorizontalAlignment="Left" VerticalAlignment="Center" 
        Focusable="True" > 
        <TextBox.Template> 
         <ControlTemplate TargetType="{x:Type TextBox}" > 
          <Border x:Name="PART_ContentHost" Focusable="False" /> 
         </ControlTemplate> 
        </TextBox.Template> 
       </TextBox> 
       <!-- Popup showing items --> 
       <Popup x:Name="Popup" Placement="Bottom" 
        Focusable="False" AllowsTransparency="True" 
        IsOpen="{TemplateBinding IsDropDownOpen}" 
        PopupAnimation="Slide" > 
        <Grid x:Name="DropDown" SnapsToDevicePixels="True" 
         MinWidth="{TemplateBinding ActualWidth}" 
         MaxHeight="{TemplateBinding MaxDropDownHeight}"> 
         <Border x:Name="DropDownBorder" Background="Transparent" Margin="0, 1, 0, 0" 
          CornerRadius="0" BorderThickness="1,1,1,1" 
          BorderBrush="{StaticResource ComboBoxNormalBorderBrush}"/> 
         <ScrollViewer Margin="4" SnapsToDevicePixels="True"> 
          <ItemsPresenter KeyboardNavigation.DirectionalNavigation="Contained" /> 
         </ScrollViewer> 
        </Grid> 
       </Popup> 
      </Grid> 
      </Border> 
      <ControlTemplate.Triggers> 
       <Trigger Property="HasItems" Value="False"> 
        <Setter Property="MinHeight" TargetName="DropDownBorder" Value="95"/> 
       </Trigger> 
       <Trigger Property="IsEnabled" Value="False"> 
        <Setter Property="TextElement.Foreground" Value="{StaticResource ComboBoxDisabledForegroundBrush}"/> 
       </Trigger> 
       <Trigger Property="IsGrouping" Value="True"> 
        <Setter Property="ScrollViewer.CanContentScroll" Value="False"/> 
       </Trigger> 
       <Trigger Property="IsEditable" Value="True"> 
        <Setter Property="KeyboardNavigation.IsTabStop" Value="False"/> 
        <Setter Property="Visibility" TargetName="PART_EditableTextBox" Value="Visible"/> 
        <Setter Property="Visibility" TargetName="ContentSite" Value="Hidden"/> 
       </Trigger> 
      </ControlTemplate.Triggers> 
     </ControlTemplate> 
    </Setter.Value> 
</Setter> 
</Style> 

ComboBoxでこのStyleを使用します。

<ComboBox Style="{DynamicResource ComboBoxStyle1}" >     
    <ComboBoxItem Content="1"/> 
    <ComboBoxItem Content="2"/> 
    <ComboBoxItem Content="3"/> 
</ComboBox> 

か:

private void ComboBox_Loaded(Object sender, RoutedEventArgs e) 
{ 
    var comboBox = sender as ComboBox; 
    var comboBoxTemplate = comboBox.Template; 
    var toggleButton = comboBoxTemplate.FindName("toggleButton", comboBox) as ToggleButton; 
    var toggleButtonTemplate = toggleButton.Template; 
    var border = toggleButtonTemplate.FindName("templateRoot", toggleButton) as Border; 

    border.Background = new SolidColorBrush(Colors.Red); 
} 
+0

私はそれを試みましたが、うまくいきません。 – MagnunStalin

+1

@MagnunStalinあなたは 'ComboBox'のデフォルトテンプレートを使用していますか? – StepUp

+0

私はそれがWindows 7のそれは完全に動作するので、それはWindows 8の問題のビジュアルスタイルだと思う。 – MagnunStalin

2

BordercmbComboコンボボックスをラップしてみてください。

その後
<Border x:Name="comboBorder" HorizontalAlignment="Left" 
      Margin="155,98,0,0" VerticalAlignment="Top"> 
    <ComboBox x:Name="cmbCombo" Width="120" /> 
</Border> 

あなたのコードビハインドで:

comboBorder.BorderThickness = new Thickness(3); 
comboBorder.BorderBrush = Brushes.OrangeRed; 
関連する問題