如何通过WPF中的触发器显示新控件

时间:2018-12-31 09:08:16

标签: wpf

现在我得到一个ModeCell,我正试图显示一个控件,可以说在网格中输入Grid来覆盖鼠标进入时的原始内容。我将StackPanel放在第一行,使其ZIndex = 10(大于Grid),并将属性StackPanel绑定到Grid的Visibility属性。这个技巧只有一个缺陷:IsMouseOver将影响网格的布局。例如,如果StackPanel的宽度最大为500,而原始Grid仅为100,则Grid会烦人地安静地扩展。这是XAML代码段

StackPanel

我尝试使用 <Grid x:Name="FileControlGrid"> !--The StackPanel to display when mouse enters--! <StackPanel Orientation="Vertical" ZIndex="10" Grid.Row="0" Visibility="{Binding ElementName=FileControlGrid, Path=IsMouseOver, Converter={StaticResource MouseoverToVisibilityCvt}}"> <...> </StackPanel> !--Origin Content below, I need the stackpanel to cover the Image--! <Image Grid.Row="0" Source="{Binding FilePath, Converter={StaticResource FileiconCvt}}" Stretch="Fill" HorizontalAlignment="Left" VerticalAlignment="Center" MaxWidth="100" MaxHeight="100" Margin="5"/> <TextBlock Grid.Row="1" Margin="0,5" Text="{Binding FileName, Mode=TwoWay}" FontFamily="Times New Roman" HorizontalAlignment="Left" FontWeight="SemiBold" MaxWidth="150" TextTrimming="WordEllipsis" FontSize="14"/> </Grid> ,但是除了设置简单的属性外,我不知道如何在触发器中生成全新的控件。有人可以帮忙吗? Images here

1 个答案:

答案 0 :(得分:0)

不明白为什么要在MouseOver中显示StackPanel的原因,但这是一个应该起作用的简单解决方案:

  • 创建Visibility ImageVisiable和StackPanelVisiable的两个属性。
  • 将两个属性都连接到控件
  • MouseEnter事件在它们之间切换

示例:

Xaml边:

<Grid x:Name="Mouse" MouseEnter="Mouse_MouseEnter" MouseLeave="Mouse_MouseLeave">
    <Grid.RowDefinitions>
        <RowDefinition Height="100"/>
        <RowDefinition Height="100"/>
    </Grid.RowDefinitions>
    <!--The StackPanel to display when mouse enters-->
    <StackPanel Orientation="Vertical" Visibility="{Binding StackPanelVisiable}"  Grid.Row="0">
    </StackPanel>
    <!--Origin Content below, I need the stackpanel to cover the Image-->
    <Image Grid.Row="0" Source="/Superman.jpg"  Visibility="{Binding ImageVisiable}"  Stretch="Fill" HorizontalAlignment="Left" VerticalAlignment="Center" MaxWidth="100" MaxHeight="100" Margin="5"/>
    <TextBlock Grid.Row="1" Margin="0,5" Text="{Binding FileName, Mode=TwoWay}" FontFamily="Times New Roman" HorizontalAlignment="Left" FontWeight="SemiBold"  MaxWidth="150" TextTrimming="WordEllipsis" FontSize="14"/>
</Grid>

查看模型:

private Visibility m_stackVisibility;
private Visibility m_imageVisibility;
public MainVM()
{
    m_stackVisibility = Visibility.Visible;
    m_imageVisibility = Visibility.Hidden;

}
public Visibility StackPanelVisiable
{
    get { return m_stackVisibility; }
    set { SetProperty(ref m_stackVisibility , value); }
}
public Visibility ImageVisiable
{
    get { return m_imageVisibility; }
    set { SetProperty(ref m_imageVisibility, value); }
}

    private void Mouse_MouseEnter(object sender, MouseEventArgs e)
    {
        ((MainVM)this.DataContext).StackPanelVisiable = Visibility.Hidden;
        ((MainVM)this.DataContext).ImageVisiable = Visibility.Visible;
    }

    private void Mouse_MouseLeave(object sender, MouseEventArgs e)
    {
        ((MainVM)this.DataContext).ImageVisiable = Visibility.Hidden;
        ((MainVM)this.DataContext).StackPanelVisiable = Visibility.Visible;
    }

希望对您有帮助

Asaf