如何在wpf datagrid文本框列中制作水印
当文本框为空时,我需要让每个单元格中的文本框都有一个水印。
我有一个datagrid和一个分组方法,用于添加到datagrid的Container类的类型。
我不是确定如何为DataGrid.TextBoxColumn添加水印样式。
这是我的代码:
MainWindow.xaml:
I need to make the textboxes in each cell have a watermark when the textbox is empty.
I have a datagrid and a grouping method in place for the type of Container class that's added to the datagrid.
I'm not sure how to add a watermark style to a DataGrid.TextBoxColumn.
Here's my code:
MainWindow.xaml:
<Window x:Class="DataGrid.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:System="clr-namespace:System;assembly=mscorlib"
xmlns:l="clr-namespace:DataGrid" Title="MainWindow" Height="350" Width="525">
<Grid>
<TabControl TabStripPlacement="Left">
<TabItem Header="Grouping">
<DataGrid ItemsSource="{Binding GroupedContainers}">
<DataGrid.GroupStyle>
<GroupStyle>
<GroupStyle.HeaderTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Path=Name}" FontWeight="Bold" Padding="3"/>
</StackPanel>
</DataTemplate>
</GroupStyle.HeaderTemplate>
<GroupStyle.ContainerStyle>
<Style TargetType="{x:Type GroupItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type GroupItem}">
<Expander>
<Expander.Header>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Path=Name}" />
<TextBlock Text="{Binding Path=ItemCount}" Margin="8,0,4,0"/>
<TextBlock Text="Items"/>
</StackPanel>
</Expander.Header>
<ItemsPresenter />
</Expander>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</GroupStyle.ContainerStyle>
</GroupStyle>
</DataGrid.GroupStyle>
</DataGrid>
</TabItem>
</TabControl>
</Grid>
</Window>
MainWindow.Xaml.cs:
MainWindow.Xaml.cs:
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.ComponentModel;
namespace DataGrid
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = new MainWindowViewModel();
}
}
public class MainWindowViewModel
{
public ICollectionView Containers { get; private set; }
public ICollectionView GroupedContainers { get; private set; }
public MainWindowViewModel()
{
var _customers = new List<Container>
{
new Container
{
Type = ContainerType.Class,
Name = "Program",
Access = AccessLevel.Public
}
};
Containers = CollectionViewSource.GetDefaultView(_customers);
GroupedContainers = new ListCollectionView(_customers);
GroupedContainers.GroupDescriptions.Add(new PropertyGroupDescription("Type"));
}
}
public enum ContainerType
{
Namespace, Class, Interface, Struct, Enum, Property, Method, Event, Expression, Variable
}
public enum AccessLevel
{
Public, Protected, Internal, Private
}
public class Container : INotifyPropertyChanged
{
private string _Name;
private ContainerType _type;
private AccessLevel _access;
public string Name
{
get { return _Name; }
set
{
_Name = value;
NotifyPropertyChanged("Name");
}
}
public ContainerType Type
{
get { return _type; }
set
{
_type = value;
NotifyPropertyChanged("Type");
}
}
public AccessLevel Access
{
get
{
return _access;
}
set
{
_access = value;
NotifyPropertyChanged("Access");
}
}
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
#endregion
#region Private Helpers
private void NotifyPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
#endregion
}
public class TypeTemplateSelector : DataTemplateSelector
{
public DataTemplate ClassTemplate { get; set; }
public DataTemplate EnumTemplate { get; set; }
public DataTemplate InterfaceTemplate { get; set; }
public override DataTemplate SelectTemplate(object item, DependencyObject container)
{
var customer = item as Container;
if (customer == null)
return base.SelectTemplate(item, container);
if (customer.Type == ContainerType.Class)
return ClassTemplate;
else if (customer.Type == ContainerType.Enum)
return EnumTemplate;
else if (customer.Type == ContainerType.Interface)
return InterfaceTemplate;
return ClassTemplate;
}
}
}
原始DataGrid示例可以在这里找到:Link
总之,这个数据网格允许用户向数据网格添加容器作为一排。每个Container按类型分组,就像c#语言一样。它假设是一个编程工具,任何帮助赞赏。
Original DataGrid sample can be found here: Link
In summary this datagrid allows users the ability to add a container to the datagrid as a row. Each Container is grouped by type, just like the c# language. It's suppose to be a programming tool, any help appreciated.
参考以下主题,你会发现很多解决方案。
1. WPF中的水印/提示文本TextBox ^ ]
2. datagrid列中的水印 [ ^ ]
Refer the following threads, you will find many solutions.
1. Watermark / hint text TextBox in WPF[^]
2. watermark in datagrid column[^]