Get DxGrid Column FieldName from within the IvalueConverter Convert Function - wpf

I have a Devexpress DxGrid with two Columns with ComboboxEdit CellTemplate
<dxg:GridControl Name="grid">
<dxg:GridControl.Columns>
<dxg:GridColumn FieldName="Size_ID" CellTemplate="{DynamicResource DimensionComboBox}"/>
<dxg:GridColumn FieldName="Color_ID" CellTemplate="{DynamicResource DimensionComboBox}"/>
</dxg:GridControl.Columns>
</dxg:GridControl>
in my app.xaml i defined a template
<DataTemplate x:Key="DimensionComboBox" >
<dxe:ComboBoxEdit x:Name="PART_Editor" DisplayMember="Grp_Property_Desc" ValueMember="Grp_Property_ID" >
<dxe:ComboBoxEdit.ItemsSource>
<Binding Path="Data">
<Binding.Converter>
<local:GridComboBoxItemSourceConverter/>
</Binding.Converter>
</Binding>
</dxe:ComboBoxEdit.ItemsSource>
</dxe:ComboBoxEdit>
</DataTemplate>
I would like to know the column field name in my converter in order for me to return data according to the column which called the converter
Public Class GridComboBoxItemSourceConverter
Implements IValueConverter
#Region "IValueConverter Members"
Public Function Convert(ByVal value As Object, ByVal targetType As Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object Implements IValueConverter.Convert
'if field name is color_id then return data for color_id
'if field name is size_id then return data for size_id
End Function
Public Function ConvertBack(ByVal value As Object, ByVal targetType As Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object Implements IValueConverter.ConvertBack
Throw New NotImplementedException()
End Function
#End Region
End Class
Is there a way to accomplish this without having to create converter class for each column, I do have many other columns to convert.

You Could create a datatemplate for each column and specify the column name in the converter parameter

Related

Very simple Datagrid example not working

SOLVED Thanks to Clemens. I just needed to add the "Property" attribute after the word "Public" in my class definition:
In VB.NET I'm trying to make a very simple Datagrid example work (i.e. an example without a database connection, etc). But no matter what I try, the Datagrid always displays the correct number of rows and the correction number of columns (and the correct column headers), but the cells are empty:
Public Class GeneratedImage
Public Property AssignmentId As Integer
Public Property DocumentPageNumber As Integer
Public Property FullPathToImage As String
Public Sub New(id As Integer, pg As Integer, docpath As String)
AssignmentId = id
DocumentPageNumber = pg
FullPathToImage = docpath
End Sub
End Class
Private Sub MainWindow_Loaded(sender As Object, e As System.Windows.RoutedEventArgs) Handles Me.Loaded
Dim myList As New List(Of GeneratedImage)
myList.Add(New GeneratedImage(1, 1, "c:\nowhere"))
myList.Add(New GeneratedImage(2, 2, "c:\nowhere2"))
GeneratedImagesInformationDatagrid.ItemsSource = myList
' GeneratedImagesInformationDatagrid.DataContext = myList
end sub
Then, in the XAML, I bind as follows:
<DataGrid x:Name="GeneratedImagesInformationDatagrid"
Height="500"
VerticalAlignment="Top"
ItemsSource="{Binding}"
Width="300">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Path=DocumentPageNumber}"
Header="Pg" />
<DataGridTextColumn Binding="{Binding Path=AssignmentId}"
Header="Assignment Id" />
<DataGridTextColumn Binding="{Binding Path=FullPathToImage}"
Header="Full Path To Image" />
</DataGrid.Columns>
</DataGrid>
I've tried it with and without the "ItemsSource="{Binding}" field.
Any help appreciated!
SOLVED Thanks to Clemens. I just needed to add the "Property" attribute after the word "Public" in my class definition:
Public Class GeneratedImage
Public Property AssignmentId As Integer
Public Property DocumentPageNumber As Integer
Public Property FullPathToImage As String
.
. etc
etc...

WPF - The value specified in System.Windows.Markup.StaticResourceHolder caused an exception

So Once again i came here to get some anwsers to problems i can't resolve them by myself, so my problem is im getting a xamlparse execption each time the button style is loaded, inside the style i have a < rectangle > that should get one color defined by the user from a query on my MVVM. So far there was no problems, the problem is trying to give that color value to the grandientstop inside the LinearGradientBrush of the rectangle. The xaml code im using:
<Rectangle x:Name="rectangle" Fill="{Binding Path=StrColor, Converter={StaticResource FadingBrushConverter}, RelativeSource={RelativeSource AncestorType={x:Type Rectangle}}}" HorizontalAlignment="Right" Height="70" Margin="0,0,0,0" RadiusY="5" RadiusX="5" VerticalAlignment="Bottom" Width="70">
<Rectangle.Effect>
<DropShadowEffect ShadowDepth="1" BlurRadius="8"/>
</Rectangle.Effect>
</Rectangle>
The "StrColor" is a Color property.
On my MVVM i have this converter:
Public Class FadingBrushConverter
Implements IValueConverter
Public Function Convert(value As Object, targetType As System.Type, parameter As Object, culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.Convert
' TODO: Do some type-checking
Dim brush = New LinearGradientBrush()
Dim color = DirectCast(value, Color)
Dim Bcolor As System.Windows.Media.Color
Bcolor.R = 0
Bcolor.G = 0
Bcolor.B = 0
brush.StartPoint = New Point(0.5, 0)
brush.EndPoint = New Point(0.5, 1.27)
brush.GradientStops.Add(New GradientStop(color, 0))
brush.GradientStops.Add(New GradientStop(Bcolor, 1))
Return brush
End Function
Public Function ConvertBack(value As Object, targetType As System.Type, parameter As Object, culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.ConvertBack
Throw New NotSupportedException()
End Function
End Class
I really dont know what am i doing wrong here, i have been looking on the internet but so far i had no luck in fixing this problem!
I also have used DynamicResource but with no success!
Thanks for any help!
I think the problem is with the binding. "Fill" property of the Rectangle is bound to the strColor property of its parent Rectangle (with the "RelativeSource FindAncestor" argument). I don't think it is realistic to assume that a rectangle is placed in another rectangle. Even if is is somehow the case, the built-in Rectangle element doesn't have a strColor property. If you want to get the color from the ViewModel, you should try something in the sort of:
{Binding Path=StrColor, Converter={StaticResource FadingBrushConverter}"
Of course, this is assuming that the DataContext is set correctly.

WPF: Placeholder

It's the following situation:
I've a WPF project with three textboxes. In these textboxes is the content something like this:
Textbox 1:
net.tcp://server1:61594/printerengine/printerserver
Textbox 2:
net.tcp://server1:61594/printerengine/printeradmin
Textbox 3:
net.tcp://server1:61594/printerengine/printeradmin
Because of the reason the paths stay the same all the time and only the server name changes, there should be an extra textbox for the server name only.
In the textbox 4 should be the server name filled in only:
Textbox 4:
server1
If you want to change the server name, it should be possible to change it in the textbox 4 and it should automatically rename the server name in the path in textbox 1-3.
I'm hoping you can understand me and give me a little bit help.
Greetings,
Lars
thank you for your answer. Not really sure if it would help me but I found this tutorial:
http://msdn.microsoft.com/de-de/library/system.windows.data.ivalueconverter.aspx
This is written in C# but I rewrote it to WPF:
This is my code:
Public Class DateConverter
Implements IValueConverter
Public Function Convert(value As Object, targetType As Type, parameter As Object) As Object
Dim [date] As DateTime = DirectCast(value, DateTime)
Return [date].ToShortDateString()
End Function
Public Function ConvertBack(value As Object, targetType As Type, parameter As Object) As Object
Dim strValue As String = value.ToString()
Dim resultDateTime As DateTime
If DateTime.TryParse(strValue, resultDateTime) Then
Return resultDateTime
End If
Return value
End Function
Public Function Convert1(value As Object, targetType As System.Type, parameter As Object, culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.Convert
End Function
Public Function ConvertBack1(value As Object, targetType As System.Type, parameter As Object, culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.ConvertBack
End Function
End Class
And this is my XAML:
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<src:DateConverter x:Key="dateConverter"/>
<TextBlock Grid.Row="2" Grid.Column="0" Margin="0,0,8,0"
Name="startDateTitle"
Style="{StaticResource smallTitleStyle}">Start Date:</TextBlock>
<TextBlock Name="StartDateDTKey" Grid.Row="2" Grid.Column="1"
Text="{Binding Path=StartDate, Converter={StaticResource dateConverter}}"
Style="{StaticResource textStyleTextBlock}"/>
</Grid>
</Window>
Not really working. Anyone who can help me? Thank you.
Greetings,
Lars
Try to use StringFormat, something like this -
<TextBlock Text="{Binding Path=Text, ElementName=Textbox4Name,
StringFormat='net.tcp://\{0\}/printerengine/printerserver' />
refer - http://elegantcode.com/2009/04/07/wpf-stringformat-in-xaml-with-the-stringformat-attribute/

WPF Button in DataGrid/ListBox

I've encountered a problem when converting a WPF project from vs2008 to vs2010.
I have a DataGrid that contains a ListBox. Each ListBoxItem has a Label and a Button. After converting to vs2010 the button no longer renders but crashes the app as soon as it comes into view. (Ie. the app loads but when the ListBox is created I get a NullReferenceException. What does work though is to remove the click event from the button and then it renders fine :) Same type of setup with Button within ListBoxItem also works when not inside a DataGrid. The content of ListBox obviously is meant to be dynamic but when working with a static collection I get the same error. Also removing the CommandParam does not help at all. Any pointers most welcome.
Code:
<DataGrid x:Name="DgTest" AutoGenerateColumns="false">
<DataGrid.Columns>
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel>
<ListBox ItemsSource="{Binding ItemList}">
<ListBox.ItemTemplate>
<DataTemplate >
<StackPanel Style="{StaticResource hzp}">
<Label />
<Button Click="Button_Click" Content="TestButton"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
Code-behind:
Imports System.Collections.ObjectModel
Class MainWindow
Public TestList As New ObservableCollection(Of TestClass)
Private Sub MainWindow_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
DgTest.ItemsSource = TestList
TestList.Add(New TestClass(0))
TestList.Add(New TestClass(1))
End Sub
Public Class TestClass
Private _ItemList As New List(Of String)
Private _id As Integer
Public Property ItemList() As List(Of String)
Get
Return _ItemList
End Get
Set(ByVal value As List(Of String))
_ItemList = value
End Set
End Property
Public Property Id() As Integer
Get
Return _id
End Get
Set(ByVal value As Integer)
_id = value
End Set
End Property
Public Sub New(ByVal id As Integer)
_ItemList.Add("String1")
_id = id
End Sub
End Class
Private Sub Button_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
End Sub
End Class
And in App Resources:
<Style TargetType="StackPanel" x:Key="hzp">
<Setter Property="Orientation" Value="Horizontal"/>
<Setter Property="Background" Value="Orange"/>
</Style>
Now here's the strange thing. If the Stackpanel Style is removed, the button will work. If the Click event for the button is removed, it will load normally.
Seems like your event handler is gone from the code-behind file, check that first. Comment if that is not the case.
I believe I have found the answer to my own question. In the ListBox bound to an ObservableCollection all Styles must be DynamicResource. Using StaticResource worked well in 3.5 but not 4! Took a few hours of randomly testing everything to find this. Case closed

WPF Hide DataGridColumn via a binding

For some reason I can't hide WPF Toolkit's DataGridColumn. I am trying to do the following:
<dg:DataGridTemplateColumn Header="Item Description" Visibility="{Binding IsReadOnly}">
<dg:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBox Text="{Binding Path=ItemDescription}" />
</DataTemplate>
</dg:DataGridTemplateColumn.CellTemplate>
This doesn't work, since it's looking for a IsReadOnly property on the ItemSource (not a property of the current class).
If add this as a property of the ItemSource class that implements INoifyPropertyChanged, it still doesn't hide the column. Is there a way around this? I want the column to hid when a button click changes IsReadOnly property.
Assume IsReadOnly returns a Visibility value and is a dependency property
I am completely stuck, I would really appreciate the help! Thanks a lot!
Posted in this question:
WPF DataGrid: Binding DataGridColumn visibility to ContextMenu MenuItems Ischeked (MVVM)
Fubzot is using the binding code similar to
Visibility='{Binding (FrameworkElement.DataContext).IsReadOnly,
RelativeSource={x:Static RelativeSource.Self}}'
You may also want to check out this:
Forwarding the DataGrid’s DataContext to its’ columns..
which is also linked in the above question.
Just for my information: Do you see any Binding errors in your Output window using your current code?
If you want to bind to the DataGridColumn's IsReadOnly property, just add a RelativeSource to the Binding (and a converter):
<BooleanToVisibilityConverter x:Key="boolToVis" />
...
<dg:DataGridTemplateColumn Header="Item Description" Visibility="{Binding IsReadOnly, RelativeSource={RelativeSource Self}, Converter={StaticResource boolToVis}}">
Also, it looks like this StackOverflow question might be related to your problem.
you need to use a converter
Public Class BooleanToVisibilityConverter
Implements IValueConverter
Public Function Convert(ByVal value As Object, ByVal targetType As System.Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object Implements IValueConverter.Convert
If targetType Is GetType(Visibility) Then
If CBool(value) = True Then
Return Visibility.Hidden
Else
Return Visibility.Visible
End If
Else
Return Nothing
End If
End Function
Public Function ConvertBack(ByVal value As Object, ByVal targetType As System.Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object Implements IValueConverter.ConvertBack
Return Nothing
End Function
End Class
then you use the converter in the XAML. SAMPLE

Resources