Click here to Skip to main content
15,909,518 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:
XML
<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:
C#
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;
        }
    }
}


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.
Posted
Updated 3-Nov-13 10:07am
v2

1 solution

Refer the following threads, you will find many solutions.

1. Watermark / hint text TextBox in WPF[^]
2. watermark in datagrid column[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900