WPF中在Grid的Row中放入Border的Style绑定问题

xujiawei29 2017-07-19 07:56:24
我在Grid的行中放入了Border,border上面放着其他的控件,现想在鼠标移动到Border中时改变Border的背景色,用触发器写了一个没反应。不知道该怎么实现?我的代码如下。
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.AnimatedTabControl.xaml"/>
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/FlatSlider.xaml"/>
<ResourceDictionary Source="Resources/Icons.xaml"/>
</ResourceDictionary.MergedDictionaries>

<Style TargetType="Grid" x:Key="GridType">
<Style.Triggers>
<Trigger Property="Grid.IsMouseOver" Value="True">
<Setter Property="Grid." Value="Red"/>
</Trigger>
</Style.Triggers>
</Style>

<local:FileNameToVisible x:Key="FileNameToVisibleConver"/>
<local:FileNameCommand x:Key="FileNameCommand"/>
<local:FileNameToFileInfo x:Key="FileNameToFileInfoConver"/>
<local:FileNameToFileSize x:Key="FileNameToFileSizeConver"/>
<local:FileNameToEnable x:Key="FileNameToEnableConver"/>
</ResourceDictionary>

</UserControl.Resources>


<Grid Margin="10">
<TabControl Name="SettingTab">

<TabItem Header="Vechle Spy" Name="VechleSpyTabItem"
controls:ControlsHelper.HeaderFontSize="10"
Selector.IsSelected="False">

<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="4*"/>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="15*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>

<Border Margin="5" Grid.Row="0" Grid.ColumnSpan="3"
Background="AliceBlue" CornerRadius="8"
Style="{StaticResource BorderType}"/>
<TextBox/>
<TabItem/>
<TabControl/>
</Grid>
...全文
1180 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
爱敲键盘的猫 2017-12-06
  • 打赏
  • 举报
回复
border是个边框,你应该用你border框内的控件事件来控制border颜色的改变。 例如你的border内是一个textBlock控件,就在textBlock控件的mouseEnter事件内写颜色控制逻辑,下面是一个简单的示例。 private void TxtLimitOrMarketPirce_MouseEnter(object sender, MouseEventArgs e) { PriceBorder.BorderBrush = new SolidColorBrush(Color.FromRgb(22, 108, 204)); } private void TxtLimitOrMarketPirce_MouseLeave(object sender, MouseEventArgs e) { PriceBorder.BorderBrush = new SolidColorBrush(Color.FromRgb(63, 63, 69)); }
Veary 2017-09-08
  • 打赏
  • 举报
回复
zipeng写的已经很清楚了,样式要针对你的Border ! 如果Border的上的内容全部覆盖了Border并且有背景颜色 那你是看不出来变化的

<Grid>
        <Grid.Resources>
            <!--Style方式-->
            <Style x:Key="BorderStyle" TargetType="Border">
                <Setter Property="Background" Value="Transparent" />
                <Style.Triggers>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter Property="Background" Value="CornflowerBlue"></Setter>
                    </Trigger>
                </Style.Triggers>
            </Style>
        </Grid.Resources>
        <Grid.RowDefinitions>
            <RowDefinition />
            <RowDefinition />
            <RowDefinition />
            <RowDefinition />
        </Grid.RowDefinitions>
        <Border Grid.Row="0" Style="{StaticResource BorderStyle}"></Border>
        <Border Grid.Row="1" Style="{StaticResource BorderStyle}"></Border>
        <Border Grid.Row="2" Style="{StaticResource BorderStyle}"></Border>
        <Border Grid.Row="3" Style="{StaticResource BorderStyle}"></Border>
    </Grid>
一个破人 2017-08-09
  • 打赏
  • 举报
回复
我觉得你想变的应该是border的边框颜色吧,背景色被Button挡住了,改变了也看不见; 把版主写的代码里边Background变为BorderBrush就能改变边框颜色;
exception92 2017-07-21
  • 打赏
  • 举报
回复
布局类控件需要事先指定一个透明的background,trigger才会更好的触发。类似:

<Window
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Width="800" Height="500" WindowStartupLocation="CenterScreen"
  xmlns:s="clr-namespace:System;assembly=mscorlib"
  Title="" SizeToContent="WidthAndHeight">
  <Window.Resources>
     <Style TargetType="Border">
        <Setter Property="Background" Value="Transparent"></Setter>
        <Style.Triggers>
           <Trigger Property="IsMouseOver" Value="True">
            <Setter Property="Background" Value="Gray"></Setter>
           </Trigger>
        </Style.Triggers>
     </Style>
  </Window.Resources>
    
  <Grid  Width="400" Height="400"> 
    <Border BorderBrush="red" BorderThickness="2">
      <Button Width="120" Height="20" Content="test"></Button>
    </Border>
  </Grid>
</Window>
xujiawei29 2017-07-21
  • 打赏
  • 举报
回复
引用 7 楼 exception11的回复:
[quote=引用 6 楼 u010562204 的回复:] [quote=引用 5 楼 exception11的回复:]布局类控件需要事先指定一个透明的background,trigger才会更好的触发。类似:

<Window
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Width="800" Height="500" WindowStartupLocation="CenterScreen"
  xmlns:s="clr-namespace:System;assembly=mscorlib"
  Title="" SizeToContent="WidthAndHeight">
  <Window.Resources>
     <Style TargetType="Border">
        <Setter Property="Background" Value="Transparent"></Setter>
        <Style.Triggers>
           <Trigger Property="IsMouseOver" Value="True">
            <Setter Property="Background" Value="Gray"></Setter>
           </Trigger>
        </Style.Triggers>
     </Style>
  </Window.Resources>
    
  <Grid  Width="400" Height="400"> 
    <Border BorderBrush="red" BorderThickness="2">
      <Button Width="120" Height="20" Content="test"></Button>
    </Border>
  </Grid>
</Window>
老铁,我试了一下,不行。我现在的情况是:一个page里,page里有一个TabControl,TabControl里放一个TabItem,TabItem里放一个Grid,Border放到Grid的行里,同一行里还有其它控件,style想让Border变颜色,做到一个选中的效果。[/quote] 那你得修改tabitem的Template了,在template的trigger中改变背景色。[/quote] 老铁,如果方便的话能不能说一下具体操作,我不太熟。。。
exception92 2017-07-21
  • 打赏
  • 举报
回复
引用 6 楼 u010562204 的回复:
[quote=引用 5 楼 exception11的回复:]布局类控件需要事先指定一个透明的background,trigger才会更好的触发。类似:

<Window
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Width="800" Height="500" WindowStartupLocation="CenterScreen"
  xmlns:s="clr-namespace:System;assembly=mscorlib"
  Title="" SizeToContent="WidthAndHeight">
  <Window.Resources>
     <Style TargetType="Border">
        <Setter Property="Background" Value="Transparent"></Setter>
        <Style.Triggers>
           <Trigger Property="IsMouseOver" Value="True">
            <Setter Property="Background" Value="Gray"></Setter>
           </Trigger>
        </Style.Triggers>
     </Style>
  </Window.Resources>
    
  <Grid  Width="400" Height="400"> 
    <Border BorderBrush="red" BorderThickness="2">
      <Button Width="120" Height="20" Content="test"></Button>
    </Border>
  </Grid>
</Window>
老铁,我试了一下,不行。我现在的情况是:一个page里,page里有一个TabControl,TabControl里放一个TabItem,TabItem里放一个Grid,Border放到Grid的行里,同一行里还有其它控件,style想让Border变颜色,做到一个选中的效果。[/quote] 那你得修改tabitem的Template了,在template的trigger中改变背景色。
xujiawei29 2017-07-21
  • 打赏
  • 举报
回复
引用 5 楼 exception11的回复:
布局类控件需要事先指定一个透明的background,trigger才会更好的触发。类似:

<Window
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Width="800" Height="500" WindowStartupLocation="CenterScreen"
  xmlns:s="clr-namespace:System;assembly=mscorlib"
  Title="" SizeToContent="WidthAndHeight">
  <Window.Resources>
     <Style TargetType="Border">
        <Setter Property="Background" Value="Transparent"></Setter>
        <Style.Triggers>
           <Trigger Property="IsMouseOver" Value="True">
            <Setter Property="Background" Value="Gray"></Setter>
           </Trigger>
        </Style.Triggers>
     </Style>
  </Window.Resources>
    
  <Grid  Width="400" Height="400"> 
    <Border BorderBrush="red" BorderThickness="2">
      <Button Width="120" Height="20" Content="test"></Button>
    </Border>
  </Grid>
</Window>
老铁,我试了一下,不行。我现在的情况是:一个page里,page里有一个TabControl,TabControl里放一个TabItem,TabItem里放一个Grid,Border放到Grid的行里,同一行里还有其它控件,style想让Border变颜色,做到一个选中的效果。
  • 打赏
  • 举报
回复
把你的style里面素有的Grid都改成Border,还有<Setter Property="Grid." Value="Red"/> 里面的属性设置的"Grid."是什么,应该是Backgroud吧。你这个样式的TargetType="Grid"设置成了Grid,给Border当然不起作用。
exception92 2017-07-20
  • 打赏
  • 举报
回复
没看到你的 BorderType 样式
xujiawei29 2017-07-20
  • 打赏
  • 举报
回复
引用 3 楼 微笑着丶的回复:
[quote=引用 2 楼 Milo米啊米啊米的回复:]把你的style里面素有的Grid都改成Border,还有<Setter Property="Grid." Value="Red"/> 里面的属性设置的"Grid."是什么,应该是Backgroud吧。你这个样式的TargetType="Grid"设置成了Grid,给Border当然不起作用。
我贴代码的时候写错了,实际上targettype="border"也没起作用。。。[/quote] 里面的Grid是我想试一下在Grid里起不起作用,结果Grid里起作用,改成Border又不起作用了。
xujiawei29 2017-07-20
  • 打赏
  • 举报
回复
引用 2 楼 Milo米啊米啊米的回复:
把你的style里面素有的Grid都改成Border,还有<Setter Property="Grid." Value="Red"/> 里面的属性设置的"Grid."是什么,应该是Backgroud吧。你这个样式的TargetType="Grid"设置成了Grid,给Border当然不起作用。
我贴代码的时候写错了,实际上targettype="border"也没起作用。。。

8,731

社区成员

发帖
与我相关
我的任务
社区描述
WPF/Silverlight相关讨论
社区管理员
  • WPF/Silverlight社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧