2013-07-02 56 views
0

我有一個定製的WpfToolkit DataGrid(.net 3.5),它始終處於編輯模式,我希望它具有與標準TextBox相同的邊框。爲我的控件使用與TextBox相同的邊框

我想:

  1. 結合我控制一個TextBox的BorderBrush的BorderBrush(似乎TextBox的BorderBrush爲空)。
  2. 在運行時讀取TextBox的BorderBrush(它始終爲空)。
  3. 我也嘗試手動設置BorderBrush,但TextBox具有 不同的邊框畫刷,具體取決於Windows主題。

一個正常的文本框邊框看起來是這樣的:

enter image description here

我的控制是這樣的,但應該有相同的邊框作爲文本框:

enter image description here

編輯:

我的DataGrid細胞風格:

<Style TargetType="{x:Type Controls:DataGridCell}" x:Key="DefaultExcelCell"> 
    <Setter Property="BorderThickness" Value="0"/> 
    <Setter Property="TextBlock.TextAlignment" Value="Right" /> 
    <Style.Triggers> 
     <Trigger Property="IsSelected" Value="True"> 
      <Setter Property="Background" Value="Transparent" /> 
      <!-- The text color of a selected cell (Black = same as not selected cell) --> 
      <Setter Property="Foreground" Value="Black" /> 
     </Trigger> 
    </Style.Triggers> 
</Style> 

回答

0

看一下默認的TextBox風格,你可以找出那裏發生了什麼。我不明白爲什麼在運行時TextBoxBorderBrush始終爲空,所以如果TextBox沒有應用其他樣式,它不應該是這樣。

<LinearGradientBrush x:Key="TextBoxBorder" EndPoint="0,20" MappingMode="Absolute" StartPoint="0,0"> 
     <GradientStop Color="#ABADB3" Offset="0.05"/> 
     <GradientStop Color="#E2E3EA" Offset="0.07"/> 
     <GradientStop Color="#E3E9EF" Offset="1"/> 
    </LinearGradientBrush> 
    <Style x:Key="TextBoxStyle1" BasedOn="{x:Null}" TargetType="{x:Type TextBox}"> 
     <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/> 
     <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.WindowBrushKey}}"/> 
     <Setter Property="BorderBrush" Value="{StaticResource TextBoxBorder}"/> 
     <Setter Property="BorderThickness" Value="1"/> 
     <Setter Property="Padding" Value="1"/> 
     <Setter Property="AllowDrop" Value="true"/> 
     <Setter Property="FocusVisualStyle" Value="{x:Null}"/> 
     <Setter Property="ScrollViewer.PanningMode" Value="VerticalFirst"/> 
     <Setter Property="Stylus.IsFlicksEnabled" Value="False"/> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="{x:Type TextBox}"> 
        <Themes:ListBoxChrome x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" RenderMouseOver="{TemplateBinding IsMouseOver}" RenderFocused="{TemplateBinding IsKeyboardFocusWithin}" SnapsToDevicePixels="true"> 
         <ScrollViewer x:Name="PART_ContentHost" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/> 
        </Themes:ListBoxChrome> 
        <ControlTemplate.Triggers> 
         <Trigger Property="IsEnabled" Value="false"> 
          <Setter Property="Background" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/> 
          <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/> 
         </Trigger> 
        </ControlTemplate.Triggers> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
     <Style.Triggers> 
      <MultiTrigger> 
       <MultiTrigger.Conditions> 
        <Condition Property="IsInactiveSelectionHighlightEnabled" Value="true"/> 
        <Condition Property="IsSelectionActive" Value="false"/> 
       </MultiTrigger.Conditions> 
       <Setter Property="SelectionBrush" Value="{DynamicResource {x:Static SystemColors.InactiveSelectionHighlightBrushKey}}"/> 
      </MultiTrigger> 
     </Style.Triggers> 
    </Style> 
+0

我確實嘗試過,但問題是每個主題都有不同的邊框樣式。例如,上述代碼在Windows 7中使用默認主題看起來很好,但是在Windows Xp或Windows 7經典主題 –

+0

中有一個爲程序中所有文本框設置的默認樣式,但它不僅設置了BorderBrush像字體,字體大小的東西。 –

+0

如何在編輯模式下自定義單元格的樣式?你有沒有嘗試設置'TextBox'風格爲{x:Null},它應該重置爲默認值。我假設'WpfToolkit DataGrid'爲某些控件定義了一些樣式。 –

相關問題