Datagrid using usercontrol
Posted
by klawusel
on Stack Overflow
See other posts from Stack Overflow
or by klawusel
Published on 2010-03-21T21:43:31Z
Indexed on
2010/03/21
21:51 UTC
Read the original article
Hit count: 432
Hello
I am fighting with this problem: I have a usercontrol which contains a textbox and a button (the button calls some functions to set the textbox's text), here is the xaml:
<UserControl x:Class="UcSelect" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Name="Control1Name" <Grid x:Name="grid1" MaxHeight="25"> <Grid.ColumnDefinitions> <ColumnDefinition /> <ColumnDefinition Width="25"/> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition Height="25"/> </Grid.RowDefinitions> <TextBox x:Name="txSelect" Text="{Binding UcText, Mode=TwoWay}" /> <Button x:Name="pbSelect" Background="Red" Grid.Column="1" Click="pbSelect_Click">...</Button> </Grid>
And here the code behind:
Partial Public Class UcSelect
Private Shared Sub textChangedCallBack(ByVal [property] As DependencyObject, ByVal args As DependencyPropertyChangedEventArgs)
Dim UcSelectBox As UcSelect = DirectCast([property], UcSelect)
End Sub
Public Property UcText() As String
Get
Return GetValue(UcTextProperty)
End Get
Set(ByVal value As String)
SetValue(UcTextProperty, value)
End Set
End Property
Public Shared ReadOnly UcTextProperty As DependencyProperty = _
DependencyProperty.Register("UcText", _
GetType(String), GetType(UcSelect), _
New FrameworkPropertyMetadata(String.Empty, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, New PropertyChangedCallback(AddressOf textChangedCallBack)))
Public Sub New()
InitializeComponent()
grid1.DataContext = Me
End Sub
Private Sub pbSelect_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
'just demo
UcText = UcText + "!"
End Sub
End Class
The UserControl works fine when used as a single control in this way:
<local:UcSelect Grid.Row="1" x:Name="ucSingle1" UcText="{Binding FirstName, Mode=TwoWay}"/>
Now I wanted to use the control in a custom datagrid column. As I like to have binding support I choosed to derive from DataGridtextColumn instead of using a DataGridTemplateColumn, here is the derived column class:
Public Class DerivedColumn Inherits DataGridTextColumn
Protected Overloads Overrides Function GenerateElement(ByVal oCell As DataGridCell, ByVal oDataItem As Object) As FrameworkElement
Dim oElement = MyBase.GenerateElement(oCell, oDataItem)
Return oElement
End Function
Protected Overloads Overrides Function GenerateEditingElement(ByVal oCell As DataGridCell, ByVal oDataItem As Object) As FrameworkElement
Dim oUc As New UcSelect
Dim oBinding As Binding = CType(Me.Binding, Binding)
oUc.SetBinding(UcSelect.UcTextProperty, oBinding)
Return oUc
End Function
End Class
The column is used in xaml in the following way:
<local:DerivedColumn Header="Usercontrol" Binding="{Binding FirstName, Mode=TwoWay}"></local:DerivedColumn>
If I start my program all seems to be fine, but changes I make in the custom column are not reflected in the object (property "FirstName"), the changes are simply rolled back when leaving the cell. I think there must be something wrong with my GenerateEditingElement code, but have no idea ... Any Help would really be appreciated
Regards Klaus
© Stack Overflow or respective owner