110,539
社区成员
发帖
与我相关
我的任务
分享
<UserControl x:Name="userControl" x:Class="WpfApp1.UserControl1"
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"
xmlns:local="clr-namespace:WpfApp1"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300" Background="#FFB6E09D">
<Grid>
<TextBlock Text="大饭店" Height="50" Width="100" Background="#FF637AC9" Foreground="White">
<TextBlock.Style>
<Style TargetType="TextBlock">
<Style.Triggers>
<DataTrigger Binding="{Binding DataContext.Value,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=UserControl}}" Value="张三">
<Setter Property="TextBlock.Background" Value="Red"/>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
</Grid>
</UserControl>
public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
this.DataContext = this;
}
public static readonly DependencyProperty ValueProperty = DependencyProperty.Register("Value",typeof(string), typeof(UserControl1), new FrameworkPropertyMetadata("熊俊"));
public string Value
{
get { return (string)GetValue(ValueProperty); }
set { SetValue(ValueProperty, value); }
}
}
<Window x:Class="WpfApp1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp1"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<local:UserControl1 x:Name="usercontrol1" Margin="0,0,261,164"/>
<Button Content="Button" HorizontalAlignment="Left" Margin="311,170,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click"/>
</Grid>
</Window>
private void Button_Click(object sender, RoutedEventArgs e)
{
usercontrol1.Value = "张三";
}
现在,上面的代码就是用自定义的依赖项属性Value了,UserControl的触发器是,当Value值变成"张三",TextBlock的背景色就变成红色。
单击窗口中的按钮,把Value值设为"张三",但我还是没有看到期望看到的红色背景。[/quote]
你把Background="#FF637AC9" 去掉就看到了。
<UserControl x:Name="userControl" x:Class="WpfApp1.UserControl1"
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"
xmlns:local="clr-namespace:WpfApp1"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300" Background="#FFB6E09D">
<Grid>
<TextBlock Text="大饭店" Height="50" Width="100" Background="#FF637AC9" Foreground="White">
<TextBlock.Style>
<Style TargetType="TextBlock">
<Style.Triggers>
<DataTrigger Binding="{Binding DataContext.Value,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=UserControl}}" Value="张三">
<Setter Property="TextBlock.Background" Value="Red"/>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
</Grid>
</UserControl>
public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
this.DataContext = this;
}
public static readonly DependencyProperty ValueProperty = DependencyProperty.Register("Value",typeof(string), typeof(UserControl1), new FrameworkPropertyMetadata("熊俊"));
public string Value
{
get { return (string)GetValue(ValueProperty); }
set { SetValue(ValueProperty, value); }
}
}
<Window x:Class="WpfApp1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp1"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<local:UserControl1 x:Name="usercontrol1" Margin="0,0,261,164"/>
<Button Content="Button" HorizontalAlignment="Left" Margin="311,170,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click"/>
</Grid>
</Window>
private void Button_Click(object sender, RoutedEventArgs e)
{
usercontrol1.Value = "张三";
}
现在,上面的代码就是用自定义的依赖项属性Value了,UserControl的触发器是,当Value值变成"张三",TextBlock的背景色就变成红色。
单击窗口中的按钮,把Value值设为"张三",但我还是没有看到期望看到的红色背景。
<UserControl x:Name="userControl" x:Class="WpfApp1.UserControl1"
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"
xmlns:local="clr-namespace:WpfApp1"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300" Background="#FFB6E09D">
<Grid>
<TextBlock Text="大饭店" Height="50" Width="100" Background="#FF637AC9" Foreground="White">
<TextBlock.Style>
<Style>
<Style.Triggers>
<DataTrigger Binding="{Binding DataContext.IsMouseOver,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=UserControl}}" Value="true">
<Setter Property="TextBlock.Background" Value="Red"/>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
</Grid>
</UserControl>
<UserControl x:Class="WpfApp1.UserControl1"
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"
xmlns:local="clr-namespace:WpfApp1"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
<TextBlock Text="大饭店" Height="50" Width="100" Background="#FF637AC9" Foreground="White">
<TextBlock.Style>
<Style TargetType="TextBlock">
<Style.Triggers>
<DataTrigger Binding="{Binding DataContext.IsMouseOver,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=UserControl}}" Value="true">
<Setter Property="TextBlock.Background" Value="Red"/>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
</Grid>
</UserControl>
public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
this.DataContext = this;
}
}
<Window x:Class="WpfApp1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp1"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<local:UserControl1/>
</Grid>
</Window>
上面是全部代码,鼠标移上去还是没有效果呢?要不,你复制代码试试
[/quote]
你#2楼回复里的 依赖性属性 呢
<UserControl x:Class="WpfApp1.UserControl1"
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"
xmlns:local="clr-namespace:WpfApp1"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
<TextBlock Text="大饭店" Height="50" Width="100" Background="#FF637AC9" Foreground="White">
<TextBlock.Style>
<Style TargetType="TextBlock">
<Style.Triggers>
<DataTrigger Binding="{Binding DataContext.IsMouseOver,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=UserControl}}" Value="true">
<Setter Property="TextBlock.Background" Value="Red"/>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
</Grid>
</UserControl>
public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
this.DataContext = this;
}
}
<Window x:Class="WpfApp1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp1"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<local:UserControl1/>
</Grid>
</Window>
上面是全部代码,鼠标移上去还是没有效果呢?要不,你复制代码试试
<UserControl x:Name="userControl" x:Class="WpfApp1.UserControl1"
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"
xmlns:local="clr-namespace:WpfApp1"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300" Background="#FFB6E09D">
<Grid>
<TextBlock Text="大饭店" Height="50" Width="100" Background="#FF637AC9" Foreground="White">
<TextBlock.Style>
<Style>
<Style.Triggers>
<DataTrigger Binding="{Binding DataContext.IsMouseOver,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=UserControl}}" Value="true">
<Setter Property="TextBlock.Background" Value="Red"/>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
</Grid>
</UserControl>
[/quote]
<UserControl x:Name="userControl" x:Class="WpfApp1.UserControl1"
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"
xmlns:local="clr-namespace:WpfApp1"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300" Background="#FFB6E09D">
<Grid>
<TextBlock Text="大饭店" Height="50" Width="100" Background="#FF637AC9" Foreground="White">
<TextBlock.Style>
<Style>
<Style.Triggers>
<DataTrigger Binding="{Binding DataContext.IsMouseOver,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=UserControl}}" Value="true">
<Setter Property="TextBlock.Background" Value="Red"/>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
</Grid>
</UserControl>
<UserControl x:Class="WpfApp1.UserControl1"
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"
xmlns:local="clr-namespace:WpfApp1"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
<Border Name="border1"/>
</Grid>
</UserControl>
假设UserControl有一个自定义的依赖性属性Value,现在需要一个触发器,当Value值为A时,border1的背景色变成红色,这个触发器该怎么写?[/quote]
写border的style,在style中用datatrigger ,不过你的binding方式要为relativesource, 找到它的祖先usercontrol,因为定义的依赖项属性是在usercontrol的上下文里。
类似这种方式:
Binding="{Binding DataContext.xxx,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=UserControl}}"
<UserControl x:Class="WpfApp1.UserControl1"
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"
xmlns:local="clr-namespace:WpfApp1"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
<Border Name="border1"/>
</Grid>
</UserControl>
假设UserControl有一个自定义的依赖性属性Value,现在需要一个触发器,当Value值为A时,border1的背景色变成红色,这个触发器该怎么写?