![](/img/trans.png)
[英]How can I bind visibility of a datagrid column on a custom wpf control?
[英]wpf - How can i control visibility on mousehover of usercontrol?
我有一個用戶控件,我想禁用UserControl中控件的可見性。 我只希望當用戶的光標懸停在用戶控件的主要部分(即“橙色”矩形部分)上時才可見。 紅色圓圈是控件的一部分,僅在“懸停”時可見
MainWindow.xaml
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525"
WindowStartupLocation="CenterScreen"
xmlns:local="clr-namespace:WpfApplication1">
<Grid>
<Canvas >
<Canvas.Background>
<VisualBrush TileMode="Tile" Stretch="Uniform" Viewport="20,20,20,20" ViewportUnits="Absolute">
<VisualBrush.Visual>
<Rectangle Width="20" Height="20" Fill="sc#1,0.01,0.01,.01" Stroke="sc#1,0.02,0.02,.02" StrokeThickness="0.1"/>
</VisualBrush.Visual>
</VisualBrush>
</Canvas.Background>
<local:ShapeNode Canvas.Left="117" Canvas.Top="84"/>
<local:ShapeNode Canvas.Left="242" Canvas.Top="183"/>
</Canvas>
</Grid>
</Window>
UserControl-ShapeNode.xaml
<UserControl x:Class="WpfApplication1.ShapeNode"
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"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
<Ellipse Fill="Red" Opacity=".2" Height="150" Width="150"></Ellipse>
<Border Margin="5" Height="50" Width="100" Background="#FFDE6119" CornerRadius="5"></Border>
<TextBlock VerticalAlignment="Center" Background="Transparent" Text="Donuts" HorizontalAlignment="Center"></TextBlock>
</Grid>
</UserControl>
我寧願使用可以在UserControl中模板化的控件。 我最喜歡的是按鈕-這是因為click事件(如果有任何用途)。 但是您可以使用其他的。
<UserControl x:Class="WpfApplication1.ShapeNode"
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"
d:DesignHeight="300" d:DesignWidth="300">
<Button>
<Button.Template>
<ControlTemplate>
<Grid x:Name="MyGrid" >
<Ellipse x:Name="MyEllipse" Visibility="Hidden" Fill="Red" Opacity=".2" Height="150" Width="150"/>
<Border Margin="5" Height="50" Width="100" Background="#FFDE6119" CornerRadius="5"></Border>
<TextBlock VerticalAlignment="Center" Background="Transparent" Text="Donuts" HorizontalAlignment="Center"></TextBlock>
</Grid>
<ControlTemplate.Triggers>
<Trigger SourceName="MyGrid" Property="IsMouseOver" Value="True">
<Setter TargetName="MyEllipse" Property="Visibility" Value="Visible"></Setter>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Button.Template>
</Button>
</UserControl>
xmlns:ei =“ http://schemas.microsoft.com/expression/2010/interactivity” xmlns:ei =“ http://schemas.microsoft.com/expression/2010/interactions”
<Grid x:Name="Grid1" Width="200" Margin="176,197,248,114" d:LayoutOverrides="HorizontalAlignment, VerticalAlignment">
<Ellipse Fill="Red" Opacity=".2" Height="150" Width="150" Margin="25,0,25,-19"/>
<Border Margin="50,40.5" Height="50" Width="100" Background="#FFDE6119" CornerRadius="5"/>
<TextBlock VerticalAlignment="Center" Background="Transparent" Text="Donuts" HorizontalAlignment="Center">
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseEnter">
<ei:ChangePropertyAction TargetObject="{Binding ElementName=Grid1}" PropertyName="Visibility">
<ei:ChangePropertyAction.Value>
<Visibility>Collapsed</Visibility>
</ei:ChangePropertyAction.Value>
</ei:ChangePropertyAction>
</i:EventTrigger>
</i:Interaction.Triggers>
</TextBlock>
</Grid>
您可以使用binding
來實現您的結果。 在ShapeNode.xaml中將橢圓的可見性綁定到Border
將border
名稱設置為例如“ border1”,並將可見性綁定設置為:
Visibility="{Binding Path=IsMouseOver, ElementName=border1, Converter={StaticResource boolToVisibillityConverter}}"
您必須創建一個轉換器才能將布爾值更改為可見性 。 使用以下轉換器:
public class BooleanToVisibilityConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
bool flag = false;
if (value is bool)
{
flag = (bool)value;
}
return (flag ? Visibility.Visible : Visibility.Hidden);
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
請注意,在這種情況下,您必須返回隱藏未折疊的狀態 。 否則,當可見性改變時,邊界也會改變位置。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.