Attached property not working - c#

I am using DevExpress control TreeListControl to display data. This control has columns, like datagrid. I want to show values in the center of the cell. For that I'm using CellTemplate:
<dxg:TreeListColumn HorizontalHeaderContentAlignment="Center" Header="January">
<dxg:TreeListColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding RowData.Row.January}" TextAlignment="Center"/>
</DataTemplate>
</dxg:TreeListColumn.CellTemplate>
</dxg:TreeListColumn>
But I have a lot of columns, and the only difference is value to display. So I decided to use a style and pass value with attached property. Style:
<Style x:Key="TreeListColumnStyle" TargetType="{x:Type dxg:TreeListColumn}">
<Setter Property="HorizontalHeaderContentAlignment" Value="Center"/>
<Setter Property="CellTemplate">
<Setter.Value>
<DataTemplate>
<TextBlock Text="{TemplateBinding ap:BenDatagridValueProperty.DataGridValue}" TextAlignment="Center"/>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
And here is my AttachedProperty:
public static class BenDatagridValueProperty
{
public static readonly System.Windows.DependencyProperty DataGridValueProperty = System.Windows.DependencyProperty.RegisterAttached(
"DataGridValue",
typeof(string),
typeof(BenDatagridValueProperty),
new System.Windows.FrameworkPropertyMetadata("", System.Windows.FrameworkPropertyMetadataOptions.Inherits));
public static string GetDataGridValue(System.Windows.DependencyObject obj)
{
return (string)obj.GetValue(DataGridValueProperty);
}
public static void SetDataGridValue(System.Windows.DependencyObject obj, string value)
{
obj.SetValue(DataGridValueProperty, value);
}
}
Remade column now looks like:
<dxg:TreeListColumn Header="January" Style="{StaticResource TreeListColumnStyle}" ap:BenDatagridValueProperty.DataGridValue="15">
15 is just a test value. And it does not sets value in datagrid columns to 15 (it does not call method SetDataGridValue(DependencyObject obj, string value). If I will write a default value in AttachedProperty, then I can see default value in cells.
Not sure where is error.

Try modify TreeListColumnStyle:
<Style x:Key="TreeListColumnStyle" TargetType="{x:Type dxg:TreeListColumn}">
<Setter Property="HorizontalHeaderContentAlignment" Value="Center"/>
<Setter Property="CellTemplate">
<Setter.Value>
<DataTemplate>
<TextBlock ap:BenDatagridValueProperty.DataGridValue="{TemplateBinding ap:BenDatagridValueProperty.DataGridValue}" Text="{Binding Path=(ap:BenDatagridValueProperty.DataGridValue), RelativeSource={RelativeSource Self}}" TextAlignment="Center"/>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>

Related

WPF Custom Base Window Class and Style

I have a an app that will open many windows, and I want all windows to look the same. I am overriding the default Windows window chrome style and making my own, so any new window that is opened (excluding messageboxes) should have the same window style. However, no matter what I seem to try it does not work. I can get it to work with one window, but when I want to make it a global style it always crashes or simply doesn't work as it should.
Here is my code:
WindowBaseStyle.xaml
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:MyProject.Styles"
xmlns:views="clr-namespace:Myproject.Views">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="GlobalStyles.xaml" />
</ResourceDictionary.MergedDictionaries>
<Style TargetType="{x:Type views:WindowBase}" BasedOn="{StaticResource {x:Type Window}}">
<Setter Property="AllowsTransparency" Value="False" />
<Setter Property="BorderBrush" Value="Red" />
<Setter Property="BorderThickness" Value="1" />
<Setter Property="WindowState" Value="Normal" />
<Setter Property="WindowStyle" Value="SingleBorderWindow" />
<Setter Property="WindowChrome.WindowChrome">
<Setter.Value>
<WindowChrome CaptionHeight="30"
UseAeroCaptionButtons="False"/>
</Setter.Value>
</Setter>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type views:WindowBase}">
<Border BorderBrush="Blue" BorderThickness="1" SnapsToDevicePixels="True">
<DockPanel Background="White" LastChildFill="True" >
<Grid Background="Blue" DockPanel.Dock="Top">
<StackPanel HorizontalAlignment="Left" Orientation="Horizontal" VerticalAlignment="Center">
<Button Name="PART_SystemMenuButton" Command="{Binding MenuCommand}" Style="{DynamicResource SystemIconButton}">
<Image Height="16" Width="16" Source="/Resources/icon.png" Stretch="Fill"/>
</Button>
<Viewbox Height="16" HorizontalAlignment="Stretch" Margin="14,2,0,0" >
<TextBlock FontSize="12" Foreground="White" Text="{Binding Title,RelativeSource={RelativeSource FindAncestor,AncestorType=Window}}" />
</Viewbox>
</StackPanel>
<StackPanel Orientation="Horizontal" WindowChrome.IsHitTestVisibleInChrome="True">
<Button x:Name="PART_MinimizeButton" Command="{Binding MinimizeCommand}" Height="30" Margin="0,0,0,0" ToolTip="Minimize" Width="45">
<Image Source="/Resources/minimize.png" Stretch="None" />
<Button.Style>
<Style TargetType="{x:Type Button}">
<Setter Property="Background" Value="#0079CB" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border Background="{TemplateBinding Background}">
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="#64AEEC"/>
</Trigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>
<Button x:Name="PART_MaximizeButton" Command="{Binding MaximizeCommand}" Height="30" Margin="0,0,0,0" Width="45">
<Image>
<Image.Style>
<Style TargetType="{x:Type Image}">
<Style.Triggers>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}, Path=WindowState}" Value="Normal">
<Setter Property="Source" Value="/Resources/maximize.png" />
<Setter Property="Stretch" Value="None" />
</DataTrigger>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}, Path=WindowState}" Value="Maximized">
<Setter Property="Source" Value="/Resources/unmaximize.png" />
<Setter Property="Stretch" Value="None" />
</DataTrigger>
</Style.Triggers>
</Style>
</Image.Style>
</Image>
<Button.Style>
<Style TargetType="{x:Type Button}">
<Setter Property="Background" Value="#0079CB" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border Background="{TemplateBinding Background}">
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="#64AEEC"/>
</Trigger>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}, Path=WindowState}" Value="Normal">
<Setter Property="ToolTip" Value="Maximize" />
</DataTrigger>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}, Path=WindowState}" Value="Maximized">
<Setter Property="ToolTip" Value="Restore Down" />
</DataTrigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>
<Button x:Name="PART_CloseButton" Command="{Binding CloseCommand}" Height="30" Margin="0,0,0,0" ToolTip="Close" Width="45" >
<Image Source="/Resources/close.png" Stretch="None" />
<Button.Style>
<Style TargetType="{x:Type Button}">
<Setter Property="Background" Value="#0079CB" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border Background="{TemplateBinding Background}">
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="Red"/>
</Trigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>
</StackPanel>
</StackPanel>
</Grid>
<!-- this ContentPresenter automatically binds to the content of the window -->
<ContentPresenter />
</DockPanel>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
WindowBase.cs
using System;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Interop;
namespace MyProject.Views
{
[TemplatePart(Name = "PART_MinimizeButton", Type = typeof(Button))]
[TemplatePart(Name = "PART_MaximizeButton", Type = typeof(Button))]
[TemplatePart(Name = "PART_CloseButton", Type = typeof(Button))]
[TemplatePart(Name = "PART_SystemMenuButton", Type = typeof(Button))]
public class WindowBase: Window
{
static WindowBase()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(CustomWindow), new FrameworkPropertyMetadata(typeof(CustomWindow)));
}
public WindowBase()
{
Loaded += (sender, evnt) =>
{
var MinimizeButton = (Button)Template.FindName("PART_MinimizeButton", this);
var MaximizeButton = (Button)Template.FindName("PART_MaximizeButton", this);
var CloseButton = (Button)Template.FindName("PART_CloseButton", this);
var SystemMenuButton = (Button)Template.FindName("PART_SystemMenuButton", this);
MinimizeButton.Click += (s, e) => WindowState = WindowState.Minimized;
MaximizeButton.Click += (s, e) => WindowState = WindowState == WindowState.Maximized ? WindowState.Normal : WindowState.Maximized;
CloseButton.Click += (s, e) => Close();
SystemMenuButton.Click += (s, e) => SystemCommands.ShowSystemMenu(this, GetMousePosition());
};
}
}
}
Window1.xaml
<local:WindowBase x:Class="MyProject.Views.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:MyProject.Views"
Height="750"
Width="1125">
<Grid>
</Grid>
</local:WindowBase>
Window1.xaml.cs
using System.Windows;
namespace MyProject.Views
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1: WindowBase
{
public Window1()
{
InitializeComponent();
}
}
}
I am overall following the MVVM pattern, and for the most part from all the articles and videos I have looked at online, they all follow this basic approach and they say it all works, but I can't seem to get it to work.
An additional note is that whenever I add my custom window control to the Window1.xaml file, it breaks the designer and says it is "Invalid Markup"
Also note I added my "WindowBaseStyle" resource dictionary to the App.xaml file as a merged resource dictionary.
Any help is greatly appreciated!! Thanks
Ok, as we discussed in the comments, it seems like the fastest solution to the problem you described is to use a StaticResource to get the window's style from a resource dictionary (or create an implicit style for windows.) I questioned the role of CustomWindow because I thought that might be causing problems with your default style override. (Remember: if you go the lookless control route and try to use the DefaultStyleKeyProperty override, you have to do this on every subclass of that control.)
However, I think doing something like this will get you reusable plumbing for multiple windows driven by viewmodels...
PopupHost
A class that would derive from your customized window. This code provides the following behaviors:
Allows the viewmodel to mark itself as having served its purpose, causing the window to close.
Allows attached properties to be specified by individual views that can affect the way the window appears on-screen, e.g. the window title.
Can be extended to notify presented items that the user has tried closing the window, allowing interception/cancellation or cleanup actions to be performed.
Code:
public class PopupHost : Window
{
private readonly AwaitableViewModelBase _viewModel;
public PopupHost(Window owner, AwaitableViewModelBase viewModel, string dataTemplateKey = null)
{
Owner = owner;
_viewModel = viewModel;
// Wrap the content in another presenter -- makes it a little easier to get to in order to look for attached properties.
var contentPresenter = new ContentPresenter
{
Content = viewModel
};
if (!string.IsNullOrWhiteSpace(dataTemplateKey))
contentPresenter.ContentTemplate = (DataTemplate) FindResource(dataTemplateKey);
Content = contentPresenter;
Task.Run(async () =>
{
await viewModel.Task;
Dispatcher.Invoke(Close);
});
Closed += ClosedHandler;
ApplyTemplate();
// Grab attached property values from the user control (or whatever element... you just need to find the descendant)
var contentElement = FindDescendantWithNonDefaultPropertyValue(contentPresenter, PopupWindowProperties.TitleProperty);
if (contentElement != null)
{
var binding = new Binding { Source = contentElement, Path = new PropertyPath(PopupWindowProperties.TitleProperty) };
SetBinding(TitleProperty, binding);
}
}
private void ClosedHandler(object sender, EventArgs args)
{
_viewModel?.Cancel();
Closed -= ClosedHandler;
}
private static Visual FindDescendant(Visual element, Predicate<Visual> predicate)
{
if (element == null)
return null;
if (predicate(element))
return element;
Visual foundElement = null;
(element as FrameworkElement)?.ApplyTemplate();
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(element); i++)
{
var visual = VisualTreeHelper.GetChild(element, i) as Visual;
foundElement = FindDescendant(visual, predicate);
if (foundElement != null)
break;
}
return foundElement;
}
private static Visual FindDescendantWithNonDefaultPropertyValue(Visual element, DependencyProperty dp)
{
return FindDescendant(element, e => !(dp.GetMetadata(e).DefaultValue ?? new object()).Equals(e.GetValue(dp)));
}
}
PopupWindowProperties
Just a dumb object containing solely attached properties so your views can convey some information to the window.
public static class PopupWindowProperties
{
public static readonly DependencyProperty TitleProperty = DependencyProperty.RegisterAttached("Title", typeof(string), typeof(PopupWindowProperties), new FrameworkPropertyMetadata(string.Empty));
public static void SetTitle(UIElement element, string value) => element.SetValue(TitleProperty, value);
public static string GetTitle(UIElement element) => element.GetValue(TitleProperty) as string;
}
AwaitableViewModelBase
A simple abstract viewmodel which has a TaskCompletionSource. This allows the popup window and the viewmodel to coordinate closing.
public abstract class AwaitableViewModelBase : ViewModelBase
{
protected TaskCompletionSource<bool> TaskCompletionSource { get; set; }
public Task<bool> Task => TaskCompletionSource?.Task;
public void RegisterTaskCompletionSource(TaskCompletionSource<bool> tcs)
{
var current = TaskCompletionSource;
if (current != null && current.Task.Status == TaskStatus.Running)
throw new InvalidOperationException();
TaskCompletionSource = tcs;
}
public virtual void Cancel() => SetResult(false);
protected void SetResult(bool result) => TaskCompletionSource?.TrySetResult(result);
}
WindowService
Last but not least, the simple service that can present the requested view and viewmodel. You can use implicit DataTemplates for your viewmodels, or provide the specific x:Key value of the template you wish to use. Note that the await doesn't really do anything here because ShowDialog blocks. We return the bool since it can be used to easily identify if the user hit OK or Cancel on a modal.
public class WindowService
{
public async Task<bool> ShowModalAsync(AwaitableViewModelBase viewModel, string dataTemplateKey = null)
{
var tcs = new TaskCompletionSource<bool>();
viewModel.RegisterTaskCompletionSource(tcs);
Application.Current.Dispatcher.Invoke(() =>
{
var currentWindow = Application.Current.Windows.OfType<Window>().SingleOrDefault(x => x.IsActive) ?? Application.Current.MainWindow;
var window = new PopupHost(currentWindow, viewModel, dataTemplateKey);
window.ShowDialog();
});
return await viewModel.Task;
}
}

Binding a command to a checkbox for onclick inside a xamdatagrid in wpf not working

I have a checkbox inside my XamDataGrid , as follows :-
<igDp:UnboundField Width="1*" Label="{LocText props:Resources.GROUPLIST_SYNC}" BindingMode="TwoWay" BindingPath="IsSynchronise.Value" Converter="{StaticResource BoolToUMDirectoryFilter}" ConverterParameter="Enabled" ToolTip="{LocText props:Resources.GROUPLIST_SYNC}">
<igDp:UnboundField.Settings>
<igDp:FieldSettings AllowEdit="True">
<igDp:FieldSettings.LabelPresenterStyle >
<Style TargetType="igDp:LabelPresenter" BasedOn="{StaticResource GmsLabelStyle }">
<Setter Property="AutomationProperties.AutomationId" Value="Group_SYNC"></Setter>
</Style>
</igDp:FieldSettings.LabelPresenterStyle>
<igDp:FieldSettings.CellValuePresenterStyle>
<Style TargetType="{x:Type igDp:CellValuePresenter}">
<Setter Property="Margin" Value="2"></Setter>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="igDp:CellValuePresenter">
<CheckBox Name="chkSynchronise" IsChecked="{Binding Path=DataContext.DataItem.IsSynchronise.Value, RelativeSource={ RelativeSource Mode=TemplatedParent}}"
HorizontalAlignment="Center" Command="{Binding SynchroniseGroups,RelativeSource={RelativeSource Mode=Self}}" HorizontalContentAlignment="Left" >
</CheckBox>
<!--<CheckBox IsChecked="{Binding Path=DataContext.DataItem.IsSynchronise.Value, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged, RelativeSource={ RelativeSource Mode=TemplatedParent}}"
Command="{Binding SynchroniseGroups,RelativeSource={RelativeSource Mode=Self}}"
HorizontalAlignment="Center" HorizontalContentAlignment="Left" >
</CheckBox>-->
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</igDp:FieldSettings.CellValuePresenterStyle>
</igDp:FieldSettings>
</igDp:UnboundField.Settings>
</igDp:UnboundField>
So, how should I bind a command to checkbox in order to work it with click and get the behaviour of checked and unchecked inside my ViewModel? Any help would be much appreciated. Thanks in advance.
At first I want to add a side note: Since infragistics release 14.2 the unbound field is obsolete (look here).
For binding a boolean to the XamDataGrid I prefer using a CheckBoxField.
Class:
public class YourClass : NotificationObject
{
private bool _checkBoxValue;
public bool CheckboxValue
{
get
{
return this._checkBoxValue;
}
set
{
if (this._checkBoxValue != value)
{
this._checkBoxValue = value;
// Do something: Event, Method, ...
this.RaisePropertyChanged(nameof(this.CheckboxValue));
}
}
}
}
XAML:
<igDp:FieldLayout>
<igDp:CheckBoxField BindingType="UseAlternateBinding"
Name="CheckerField"
Label="YourCheckerFieldLabel"
ToolTip="YourTooltip"
AlternateBinding="{Binding CheckboxValue, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
/>
</igDp:FieldLayout>

Change property of component from template based on parent

I want to use the same style for two controls, just change two background colors that are deep in its hierarchy.
I have a TextBox, a style for its ScrollViewer, a style and a template for its ScrollBar with a Background for its Border and a style for its Thumb with a Background for its Border.
How can I change The two backgrounds (programmatically or not) using only the TextBox?
.........
..........
..........
<!-- ScrollBar Style -->
<Style x:Key="{x:Type ScrollBar}" TargetType="ScrollBar">
....
<Style.Triggers>
<Trigger Property="Orientation" Value="Horizontal">
....
<Setter Property="Template" Value="{StaticResource HorizontalScrollBarTemplate}"/>
....
</Trigger>
</Style.Triggers>
</Style>
<!-- Horizontal Scrollbar Template -->
<ControlTemplate x:Key="HorizontalScrollBarTemplate" TargetType="{x:Type ScrollBar}">
<Grid Background="{StaticResource ScrollBackroundBrush}">
<Track Name="PART_Track">
<Track.Thumb>
<Thumb Style="{StaticResource ScrollBarThumb}"/>
</Track.Thumb>
</Track>
</Grid>
</ControlTemplate>
<!-- Thumb Style -->
<Style x:Key="ScrollBarThumb" TargetType="{x:Type Thumb}">
....
<Setter Property="Template">
....
<Border
....
Background="{StaticResource ScrollThumbBrush}"/>
....
</Setter>
</Style>
Is there a way to change the two backgrounds (to use this color and another same scrollviewer on another control perhaps with another color) without rewriting the whole code?
For example to have its style and write:
TextBox tb1 = new TextBox();
tb1.Style = /*style*/
tb1.BackgroundOfScroll = /*brush11*/
tb1.BackgroundOfThumb = /*brush12*/
TextBox tb2 = new TextBox();
tb2.Style = /*style*/
tb2.BackgroundOfScroll = /*brush21*/
tb2.BackgroundOfThumb = /*brush22*/
I made corresponding example. If you had only one color to set you could use Tag property since it is DependencyProperty. Create then two attached properties and get to them as follows:
<Button local:Color.CustomBackground="CadetBlue">
<Button.Template>
<ControlTemplate TargetType="Button">
<ContentPresenter>
<ContentPresenter.ContentTemplate>
<ItemContainerTemplate>
<Border Width="50" Height="{Binding RelativeSource={RelativeSource Self}, Path=Width}"
Background="{Binding RelativeSource={RelativeSource AncestorType=Button}, Path=(local:Color.CustomBackground)}">
</Border>
</ItemContainerTemplate>
</ContentPresenter.ContentTemplate>
</ContentPresenter>
</ControlTemplate>
</Button.Template>
</Button>
C#
public class Color : DependencyObject
{
private static readonly DependencyProperty CustomBackgroundProperty =
DependencyProperty.RegisterAttached("CustomBackground", typeof(SolidColorBrush), typeof(Color),
new PropertyMetadata(null));
public static void SetCustomBackground(DependencyObject obj, SolidColorBrush color)
{
obj.SetValue(CustomBackgroundProperty, color);
}
public static SolidColorBrush GetCustomBackground(DependencyObject obj)
{
return (SolidColorBrush)obj.GetValue(CustomBackgroundProperty);
}
}

Get Value from a Label in a Template for a Listboxitem

My problem is that I need the Value of a Label in a Template as a string. Its a Template for a ListBoxItem with a CheckBox and a Label. When the box get checked I need the Value of the Label as a string variable in the code.
ListBox
<ListBox HorizontalAlignment="Left" Height="196" Margin="10,24,0,0"
VerticalAlignment="Top" Width="155" BorderBrush="#FF7799BB"
Name="ListBoxTblColumns" DataContextChanged="TableColumns_DataContextChanged"
ItemsSource="{Binding Columns}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<CheckBox Name="columnSelectBox" Margin="0,7,4,0" Checked="columnSelectBox_Checked" Unchecked="columnSelectBox_Unchecked"/>
<Label Name="lblColName" Content="{Binding}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
<ListBox.ItemContainerStyle>
<Style TargetType="{x:Type ListBoxItem}">
<Setter Property="Focusable" Value="False"/>
</Style>
</ListBox.ItemContainerStyle>
</ListBox>
And in this event I want to get the value from lblColName:
public void columnSelectBox_Checked(object sender, RoutedEventArgs e)
{
string colName = ...;
}
Ideas?

TextWrapping textbox in WPF DataGrid

I've been trying to make my textboxes in my DataGrid wrap. I got it working but it seems to break the Text binding.
XAML
<DataGrid x:Name="dataGrid" Grid.Row="0" AutoGenerateColumns="False" ItemsSource="{Binding}">
<!-- <DataGrid.CellStyle>
<Style TargetType="DataGridCell">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type DataGridCell}">
<TextBox TextWrapping="Wrap" Text="{Binding Path=Value}">
</TextBox>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</DataGrid.CellStyle> -->
</DataGrid>
I add the columns and rows using a data set like so.
CS
#region Variables
private DataTable m_stringData = new DataTable();
private DataSet m_stringDataSet = new DataSet();
#endregion
#region Constructors
public LocEditor()
{
InitializeComponent();
AddColumn("ID", 100);
AddString("Test");
dataGrid.DataContext = m_stringData;
}
#endregion
#region Methods
private void AddColumn(string l_columnName, int l_iWidth)
{
m_stringData.Columns.Add(l_columnName, typeof(string));
dataGrid.Columns.Add(new DataGridTextColumn
{
Header = l_columnName,
Width = l_iWidth,
Binding = new Binding(l_columnName)
//Binding = new Binding(string.Format("[{0}]", l_columnName))
});
}
private void AddString(string l_stringID)
{
m_stringData.Rows.Add();
m_stringData.Rows[m_stringData.Rows.Count - 1][0] = l_stringID;
}
#endregion
Any help would be greatly appreciated.
Figured it out. By setting the Element and EditingElementStyle, I don't have to reset the Binding.
DataGridTextColumn l_column = new DataGridTextColumn();
l_column.Header = l_columnName;
l_column.Binding = new Binding(l_columnName);
l_column.Width = l_iWidth;
Style l_textStyle = new Style(typeof(TextBlock));
l_textStyle.Setters.Add(new Setter(TextBlock.TextWrappingProperty, TextWrapping.Wrap));
l_column.ElementStyle = l_textStyle;
Style l_textEditStyle = new Style(typeof(TextBox));
l_textEditStyle.Setters.Add(new Setter(TextBox.TextWrappingProperty, TextWrapping.Wrap));
l_column.EditingElementStyle = l_textEditStyle;
dataGrid.Columns.Add(l_column);
This inserts the GridViewColumn directly into the GridView, but it worked for me.
<ListView Name="myListView">
<ListView.View>
<GridView>
<GridViewColumn Header="HEADER NAME" x:Name="header">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBox TextWrapping="Wrap" Text="{Binding Path=Value}" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
In order to see the wrapped text fully, you will need to set the height of the rows to be large enough to see this (or do height conversions dynamically).
<My:ToHeightConverter x:Key="heightConverter" />
<Style TargetType="ListViewItem">
<Setter Property="Height" Value="{Binding ElementName=myListView, Path=ActualHeight, Converter={StaticResource heightConverter}}" />
</Style>
And then in the code behind the GridView:
[ValueConversion(typeof(double), typeof(double))]
public class ToHeightConverter : IValueConverter {
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
return (((double)value * 10); //return the height wanted here
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
throw new NotImplementedException();
}
}
If you're looking to just add TextWrapping to all TextBoxes in your DataGrid, I would suggest making an implicit style for them in your DataGrid.Resources
<DataGrid.Resources>
<Style TargetType="{x:Type TextBox}">
<Setter Property="TextWrapping" Value="Wrap" />
</Style>
</DataGrid.Resources>
The reason why the data isn't showing up in your Template is because you're missing the ContentPresenter. This is the object that displays the rendered content of the actual DataGridCell. The DataGridCell itself has no idea what the content of its cell is, so doesn't know what the binding is.
For example, this works
<Style TargetType="DataGridCell">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type DataGridCell}">
<TextBlock TextWrapping="Wrap">
<ContentPresenter Content="{TemplateBinding Content}" />
</TextBlock>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
But not this
<Style TargetType="DataGridCell">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type DataGridCell}">
<TextBox TextWrapping="Wrap" Text="{TemplateBinding Content}" />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
because the Content is whatever object happens to be in the cell at the time, whether it is a TextBox, TextBlock, ComboBox, etc

Categories

Resources