Click here to Skip to main content
15,880,725 members
Please Sign up or sign in to vote.
1.44/5 (2 votes)
See more:
Would appreciate help to get this code to work.

I'm using VisualStudio 2012, using WPF with VB.NET.

I have a namespace named MyNameSpace. It's defined at the solution level

It has a number of projects within it.

I'm trying to get a ValueConverter to be recognized so that I can use it.

Purpose is to convert a value of type DateTime, which includes date and time, to simply MM/dd/yyyy

Below is code for a window in one project, followed by the window's code-behind.

Thank you very much.

MyWin.XAML:

XML
<Window x:Class="MyWin"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MyWin" Height="300" Width="300"
    xmlns:local="clr-namespace:MyNameSpace">
    <Window.Resources>
        <!-- ERROR ON NEXT LINE: The name DateConverter does not exist in the namespace-->
        <local:DateConvertor x:Key="MyDateConverter"/>
    </Window.Resources>
    <Grid>
        <DataGrid>
            <DataGrid.Columns>
                <DataGridTemplateColumn  Width="auto" Header="Do By">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock Margin="4" Text="{Binding Path=DoByDate}" />
                            <!-- Cannot use the converter to show MM/dd/yyyy.-->
<!--                        <TextBlock Margin="4" Text="{Binding Path=DoByDate, Converter={StaticResource DateConvertor}}" /> -->
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                    <DataGridTemplateColumn.CellEditingTemplate>
                        <DataTemplate>
                            <DatePicker SelectedDate="{Binding Path=DoByDate, Mode=TwoWay}" />
                        </DataTemplate>
                    </DataGridTemplateColumn.CellEditingTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>
        </DataGrid>
    </Grid>
</Window>


MyWin.XAML.VB:

VB
Imports System.Globalization
Public Class MyWin
    Public Class DateConvertor
        Implements IValueConverter
        Public Function Convert(value As Object, targetType As Type, parameter As Object, culture As System.Globalization.CultureInfo) As Object Implements IValueConverter.Convert
            Return DirectCast(value, DateTime).ToString("MM/dd/yyyy")
        End Function
        Public Function ConvertBack(value As Object, targetType As Type, parameter As Object, culture As System.Globalization.CultureInfo) As Object Implements IValueConverter.ConvertBack
            Return DateTime.ParseExact(value.ToString(), "ddMMyyyy", CultureInfo.InvariantCulture)
        End Function
    End Class
    Public Sub New()
        ' This call is required by the designer.
        InitializeComponent()
        ' Add any initialization after the InitializeComponent() call.
    End Sub
End Class
Posted
Updated 16-Aug-13 9:26am
v3

If it is in another assembly, then you have to add the assembly informaton. i.e.:

xmlns:Common="clr-namespace:Fox.UI.Resource;assembly=Fox.UI.Resource"
 
Share this answer
 
Comments
Member 10191023 16-Aug-13 16:40pm    
It's in the same assembly.
Clifford Nelson 16-Aug-13 16:48pm    
So why do you point out you have multiple projects. If this is all in the same project... What is in the other projects?
HTML
xmlns:local="clr-namespace:LSE"
what is LSE, if you write that you use namespace MyNameSpace? Use MyNameSpace instead of LSE
 
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