wpf datagridcell元素可见性

时间:2015-10-12 15:27:28

标签: c# wpf xaml

当数据网格中的鼠标位于行上时,是否可以更改celltemplate中的按钮可见性。例如,当MouseIsOver=True可见性TrueMouseIsOver=False可见性False时。我该怎么用触发器? * *

的Xaml:

<Window x:Class="WpfApplication3.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">
<Grid>
    <DataGrid ItemsSource="{Binding Items}" AutoGenerateColumns="False">
        <DataGrid.Columns>
            <DataGridTemplateColumn Header="Name">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <Label Content="{Binding .}"></Label>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
            <DataGridTemplateColumn Header="Button">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <Button Visibility="Hidden">OK</Button>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
        </DataGrid.Columns>
    </DataGrid>
</Grid>

代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApplication3
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        DataContext = new Data();
    }
}

public class Struct
{
    public string Name { get; set; }
}

public class Data
{
    public List<string> Items { get; set; }
    public Data()
    {
        Items = new List<string>();
        Items.Add("First");
        Items.Add("Second");
        Items.Add("Third");
    }
}
}
  • *

1 个答案:

答案 0 :(得分:0)

如果您的按钮被隐藏,则无法悬停。

在Visual Studio或Blend中,更改按钮的样式/模板以使ContentPresenter不可见:

在窗口中放置一个临时按钮,右键单击“编辑样式”/“编辑副本” 将样式命名为“InvisibleButtonStyle”。

在触发器部分,添加:

<Trigger Property="IsMouseOver" Value="false">
    <Setter Property="Background" TargetName="border" Value="Transparent"/>
    <Setter Property="BorderBrush" TargetName="border" Value="Transparent"/>
    <Setter Property="Foreground" Value="Transparent"/>
</Trigger> 

然后销毁临时按钮,并将样式应用于DataGrid按钮:

<Button Style="{StaticResource InvisibleButtonStyle}">OK</Button>

就是这样,快乐的编码