Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

Struggling with Name Space to get a Value Converter to Work

$
0
0

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:
 
<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:
 
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


Harlan Black


Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>