private void SampleDataGrid_AutoGeneratingColumn(object sender, AutoGeneratingColumnArgs e)
{
e.Column.AllowEditing = e.Column.MappingName == "ItemType" || e.Column.MappingName == "Value";
if (e.Column.MappingName == "ItemName" || e.Column.MappingName == "ItemShortName" || e.Column.MappingName == "ItemType")
{
e.Column.CellStyle = Application.Current.FindResource("DarkGrayStyle") as Style;
}
if (e.Column.MappingName == "Value")
{
e.Column = new GridTemplateColumn() { MappingName = "Value" };
e.Column.CellTemplateSelector = new DataTemplateSelectorExt();
}
}
public class DataTemplateSelectorExt : DataTemplateSelector
{
public override DataTemplate SelectTemplate(object item, DependencyObject container)
{
if (item == null || container == null)
return base.SelectTemplate(item, container);
if ((item as DataItem).ItemType ==1)
{
var factory1 = new FrameworkElementFactory(typeof(TextBlock));
factory1.SetBinding(TextBlock.TextProperty, new Binding("ItemName"));
factory1.SetValue(TextBlock.ForegroundProperty, Brushes.Black);
factory1.SetValue(TextBlock.BackgroundProperty, Brushes.Aquamarine);
return new DataTemplate { VisualTree = factory1 };
}
else if((item as DataItem).ItemType == 2)
{
var factory2 = new FrameworkElementFactory(typeof(TextBox));
factory2.SetBinding(TextBox.TextProperty, new Binding("ItemShortName"));
factory2.SetValue(TextBox.ForegroundProperty, Brushes.Black);
factory2.SetValue(TextBox.IsReadOnlyProperty, true);
factory2.SetValue(TextBox.BackgroundProperty, Brushes.Coral);
return new DataTemplate { VisualTree = factory2 };
}
else
{
var factory3 = new FrameworkElementFactory(typeof(TextBox));
factory3.SetBinding(TextBox.TextProperty, new Binding("ItemShortName"));
factory3.SetValue(TextBox.ForegroundProperty, Brushes.Black);
factory3.SetValue(TextBox.BackgroundProperty, Brushes.Yellow);
return new DataTemplate { VisualTree = factory3 };
}
return base.SelectTemplate(item, container);
}
} |
factory3.SetValue(Syncfusion.UI.Xaml.Grid.FocusManagerHelper.FocusedElementProperty, true); |
e.Column.DisplayBinding = new Binding(mName) { Converter = new Round2ZeroDigitsConverter(), StringFormat = "N0", ConverterCulture = CultureInfo.CurrentCulture };
factory2.SetValue(TextBox.HorizontalContentAlignmentProperty,HorizontalAlignment.Right; |
factory2.AddHandler(TextBox.GotFocusEvent, new RoutedEventHandler(gotFocus));
private void gotFocus(object sender, RoutedEventArgs e)
{
TextBox tb = (sender as TextBox);
if (tb != null)
{
tb.Dispatcher.BeginInvoke(new Action(() =>
{
tb.SelectAll();
}), System.Windows.Threading.DispatcherPriority.ApplicationIdle);
}
} |
factory2.SetBinding(TextBox.TextProperty, new Binding("ItemShortName") { Converter = new NumberRounOffConverter()});
public class NumberRounOffConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
//Write your code here
return value;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return value;
}
} |
public class DataTemplateSelectorExt : DataTemplateSelector
{
public override DataTemplate SelectTemplate(object item, DependencyObject container)
{
if (item == null || container == null)
return base.SelectTemplate(item, container);
if ((item as DataItem).ItemType == 1)
{
var factory1 = new FrameworkElementFactory(typeof(TextBlock));
factory1.SetBinding(TextBlock.TextProperty, new Binding("ItemName"));
factory1.SetValue(TextBlock.ForegroundProperty, Brushes.Black);
factory1.SetValue(TextBlock.BackgroundProperty, Brushes.Aquamarine);
return new DataTemplate { VisualTree = factory1 };
}
else if ((item as DataItem).ItemType == 2)
{
var factory2 = new FrameworkElementFactory(typeof(TextBox));
factory2.SetBinding(TextBox.TextProperty, new Binding("ItemShortName"));
factory2.SetValue(TextBox.ForegroundProperty, Brushes.Black);
factory2.SetValue(TextBox.IsReadOnlyProperty, true);
factory2.SetValue(TextBox.HorizontalContentAlignmentProperty, HorizontalAlignment.Right);
factory2.SetValue(Syncfusion.UI.Xaml.Grid.FocusManagerHelper.FocusedElementProperty, true);
factory2.SetValue(TextBox.BackgroundProperty, Brushes.Coral);
factory2.AddHandler(TextBox.GotFocusEvent, new RoutedEventHandler(ReadOnlyTextBox_GotFocus));
return new DataTemplate { VisualTree = factory2 };
}
else
{
var factory3 = new FrameworkElementFactory(typeof(TextBox));
factory3.SetBinding(TextBox.TextProperty, new Binding("Value") { Converter = new NumberRounOffConverter() });
factory3.SetValue(TextBox.ForegroundProperty, Brushes.Black);
factory3.SetValue(TextBox.HorizontalContentAlignmentProperty, HorizontalAlignment.Right);
factory3.SetValue(Syncfusion.UI.Xaml.Grid.FocusManagerHelper.FocusedElementProperty, true);
factory3.SetValue(TextBox.BackgroundProperty, Brushes.Yellow);
factory3.AddHandler(TextBox.GotFocusEvent, new RoutedEventHandler(EditTextBox_GotFocus));
factory3.AddHandler(TextBox.LostFocusEvent, new RoutedEventHandler(EditTextBox_LostFocus));
return new DataTemplate { VisualTree = factory3 };
}
return base.SelectTemplate(item, container);
}
//To display value with decimal digits when focusing.
private void EditTextBox_GotFocus(object sender, RoutedEventArgs e)
{
TextBox tb = (sender as TextBox);
tb.SetBinding(TextBox.TextProperty, new Binding("Value"));
tb.Dispatcher.BeginInvoke(new Action(() =>
{
tb.SelectAll();
}), System.Windows.Threading.DispatcherPriority.ApplicationIdle);
}
//To display value by rounding off after editing
private void EditTextBox_LostFocus(object sender, RoutedEventArgs e)
{
TextBox tb = (sender as TextBox);
tb.SetBinding(TextBox.TextProperty, new Binding("Value") { Converter = new NumberRounOffConverter() });
}
private void ReadOnlyTextBox_GotFocus(object sender, RoutedEventArgs e)
{
TextBox tb = (sender as TextBox);
tb.Dispatcher.BeginInvoke(new Action(() =>
{
tb.SelectAll();
}), System.Windows.Threading.DispatcherPriority.ApplicationIdle);
}
} |
private void EditTextBox_GotFocus(object sender, RoutedEventArgs e)
{
TextBox tb = (sender as TextBox);
tb.Dispatcher.BeginInvoke(new Action(() =>
{
tb.SetBinding(TextBox.TextProperty, new Binding("Value") { Converter = new NullConverter()});
tb.SelectAll();
}), System.Windows.Threading.DispatcherPriority.ApplicationIdle);
}
public class NullConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value == null)
return string.Empty;
return value;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value == null)
return value;
if (string.IsNullOrEmpty(value.ToString()))
return null;
return value;
}
}
|