xaml
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:ivaluecon"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Window.Resources>
<local:YesNoToBooleanConverter x:Key="YesNoToBooleanConverter" />
</Window.Resources>
<Grid>
<DataGrid x:Name="dg" Grid.Row="1" AutoGenerateColumns="False" HorizontalAlignment="Left" Height="163" Margin="25,61,0,0" VerticalAlignment="Top" Width="744">
<DataGridTextColumn Binding="{Binding d, Converter={StaticResource YesNoToBooleanConverter}}">
</DataGridTextColumn>
</DataGrid>
<DatePicker x:Name="sdate" HorizontalAlignment="Left" Height="33" Margin="66,312,0,0" VerticalAlignment="Top" Width="148"/>
<DatePicker x:Name="edate" HorizontalAlignment="Left" Height="33" Margin="272,313,0,0" VerticalAlignment="Top" Width="148"/>
<Button x:Name="button1" Click="button1_Click" Content="Button" HorizontalAlignment="Left" Height="41" Margin="452,308,0,0" VerticalAlignment="Top" Width="155"/>
</Grid>
I am not able to understand how to bind datelist columns.
class
Public Class YesNoToBooleanConverter
Implements IValueConverter
Public Function Convert(value As Object, targetType As Type, parameter As Object, culture As Globalization.CultureInfo) As Object Implements IValueConverter.Convert
If value.tstring() = "true" Then
Return "Yes"
Else
Return "No"
End If
End Function
Public Function ConvertBack(value As Object, targetType As Type, parameter As Object, culture As Globalization.CultureInfo) As Object Implements IValueConverter.ConvertBack
Return New Object()
End Function
Here is my code for generating dateslist columns
Class MainWindow
Private Sub button1_Click(sender As Object, e As RoutedEventArgs)
Dim daysList As DataTable = New DataTable()
Dim d As DateTime = New DateTime(2020, 12, 1)
While d <= New DateTime(2020, 12, 14)
Dim columnName As String = d.ToString("dd.MM")
daysList.Columns.Add(columnName, GetType(Boolean))
Dim dd As DataGridTextColumn = New DataGridTextColumn() With {
.Header = columnName,
.Binding = New Binding("[" & columnName & "]")
}
dg.Columns.Add(dd)
d = d.AddDays(1)
End While
Dim truth = String.Format("{0:on;0;OFF}", True.GetHashCode())
Dim unTruth = String.Format("{0:on;0;OFF}", False.GetHashCode())
For r As Integer = 0 To 1 - 1
Dim row = daysList.NewRow()
For c As Integer = 0 To daysList.Columns.Count - 1
row(c) = (c Mod (r + 1)) = 0
Next
daysList.Rows.Add(row)
Next
dg.ItemsSource = daysList.DefaultView
End Sub
End Class
Someone help me how to bind datelist column with xaml. Please have a look once and give me any example on this issue to fix this. thankyou....
below is the screenshot of dateslist and columns are having True and False I need to change as Yes and No.
enter image description here
Related
xaml
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:ivaluecon"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Window.Resources>
<local:YesNoToBooleanConverter x:Key="YesNoToBooleanConverter" />
</Window.Resources>
<Grid>
<DataGrid x:Name="dg" Grid.Row="1" AutoGenerateColumns="False" HorizontalAlignment="Left" Height="163" Margin="25,61,0,0" VerticalAlignment="Top" Width="744">
<DataGridTextColumn Binding="{Binding d, Converter={StaticResource YesNoToBooleanConverter}}">
</DataGridTextColumn>
</DataGrid>
<DatePicker x:Name="sdate" HorizontalAlignment="Left" Height="33" Margin="66,312,0,0" VerticalAlignment="Top" Width="148"/>
<DatePicker x:Name="edate" HorizontalAlignment="Left" Height="33" Margin="272,313,0,0" VerticalAlignment="Top" Width="148"/>
<Button x:Name="button1" Click="button1_Click" Content="Button" HorizontalAlignment="Left" Height="41" Margin="452,308,0,0" VerticalAlignment="Top" Width="155"/>
</Grid>
I am not able to understand how to bind datelist columns.
class
Public Class YesNoToBooleanConverter
Implements IValueConverter
Public Function Convert(value As Object, targetType As Type, parameter As Object, culture As Globalization.CultureInfo) As Object Implements IValueConverter.Convert
If value.tstring() = "true" Then
Return "Yes"
Else
Return "No"
End If
End Function
Public Function ConvertBack(value As Object, targetType As Type, parameter As Object, culture As Globalization.CultureInfo) As Object Implements IValueConverter.ConvertBack
Return New Object()
End Function
Here is my code for generating dateslist columns
Class MainWindow
Private Sub button1_Click(sender As Object, e As RoutedEventArgs)
Dim daysList As DataTable = New DataTable()
Dim d As DateTime = New DateTime(2020, 12, 1)
While d <= New DateTime(2020, 12, 14)
Dim columnName As String = d.ToString("dd.MM")
daysList.Columns.Add(columnName, GetType(Boolean))
Dim dd As DataGridTextColumn = New DataGridTextColumn() With {
.Header = columnName,
.Binding = New Binding("[" & columnName & "]")
}
dg.Columns.Add(dd)
d = d.AddDays(1)
End While
Dim truth = String.Format("{0:on;0;OFF}", True.GetHashCode())
Dim unTruth = String.Format("{0:on;0;OFF}", False.GetHashCode())
For r As Integer = 0 To 1 - 1
Dim row = daysList.NewRow()
For c As Integer = 0 To daysList.Columns.Count - 1
row(c) = (c Mod (r + 1)) = 0
Next
daysList.Rows.Add(row)
Next
dg.ItemsSource = daysList.DefaultView
End Sub
End Class
Someone help me how to bind datelist column with xaml. Please have a look once and give me any example on this issue to fix this. thankyou....
below is the screenshot of dateslist and columns are having True and False I need to change as Yes and No.
enter image description here
below code not matching the Headerdatecolumns with sql inam1(Table) Indate Column, I am not able to fix this problem someone help me to fix this problem
this screenshot is the datatable
this screenshot is inam(table)
this screenshot is giving result
Class MainWindow
Dim connection As New SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=D:\BUTTONFORMWPF1 - COPY\BUTTONFORMWPF1\NEWDATABASE.MDF;Integrated Security=True;Connect Timeout=30;User Instance=True")
Private Sub Button_Click_1(sender As Object, e As RoutedEventArgs)
Dim table As DataTable = New DataTable("record")
Dim col = New DataGridTextColumn()
col.Header = "Column1"
col.Binding = New Binding("[0]")
dg.Columns.Add(col)
col = New DataGridTextColumn()
col.Header = "Column2"
col.Binding = New Binding("[1]")
dg.Columns.Add(col)
col = New DataGridTextColumn()
Dim startDate As Date = CType(dtpStartDate.SelectedDate, DateTime)
Dim endDate As Date = CType(dtpEndDate.SelectedDate, DateTime)
While startDate <= endDate
Dim NewColumn As New DataGridTextColumn()
NewColumn.Header = startDate.ToShortDateString()
NewColumn.Binding = New Binding("[2]")
dg.Columns.Add(NewColumn)
startDate = startDate.AddDays(1)
End While
Dim rows As List(Of Object) = New List(Of Object)()
Dim value As String()
value = New String(2) {}
value(0) = "ID"
value(1) = "Name"
value(2) = "AM"
rows.Add(value)
Dim sda As SqlDataAdapter = New SqlDataAdapter("Select datatest1.ID, datatest1.Name, inam1.AM from datatest1 inner join inam1 On datatest1.ID=inam1.ID", connection)
For i As Integer = 0 To table.Columns.Count
sda.Fill(table)
rows.Add(table(i))
Next i
dg.ItemsSource = rows
End Sub
End Class
below is the xaml
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:project1"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<DataGrid Name="dg" CanUserAddRows="True"
AutoGenerateColumns="False"
ItemsSource="{Binding}" Margin="293,153,122,120" >
</DataGrid>
<DatePicker x:Name="dtpStartDate" Grid.Row="0" HorizontalAlignment="Left" Height="24" Margin="53,177,0,0" VerticalAlignment="Top" Width="127"/>
<DatePicker x:Name="dtpEndDate" HorizontalAlignment="Left" Height="25" Margin="47,241,0,0" VerticalAlignment="Top" Width="139"/>
<Button x:Name="Button" Click="Button_Click_1" Content="Button" HorizontalAlignment="Left" Height="26" Margin="78,310,0,0" VerticalAlignment="Top" Width="110"/>
</Grid>
If I run my project, my tooltip converter is run once - I need it to run each time the mouse hoover over a row.
Here's my XAML:
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApplication1"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<local:MyTooltipConverter x:Key="MyTooltipConverter" />
</Window.Resources>
<Grid>
<DataGrid x:Name="dataGrid" ItemsSource="{Binding}" HorizontalAlignment="Left" VerticalAlignment="Top" Height="263" Width="507">
<DataGrid.Resources>
<Style TargetType="{x:Type DataGridRow}">
<Setter Property="ToolTip">
<Setter.Value>
<ToolTip Content="{Binding ??, Converter={StaticResource MyTooltipConverter}}" />
</Setter.Value>
</Setter>
</Style>
</DataGrid.Resources>
</DataGrid>
</Grid>
</Window>
And code...
Imports System.Globalization
Class MainWindow
Public Class Person
Public Property Name As String
End Class
Public Persons As New List(Of Person)
Private Sub MainWindow_Loaded(sender As Object, e As RoutedEventArgs) Handles Me.Loaded
For i As Integer = 0 To 5
Persons.Add(New Person With {.Name = "Test " + i.ToString})
Next
dataGrid.DataContext = Persons
End Sub
End Class
Public Class MyTooltipConverter
Implements IValueConverter
Public Function Convert(value As Object, targetType As Type, parameter As Object, culture As CultureInfo) As Object Implements IValueConverter.Convert
If value Is Nothing Then
Return Nothing
End If
Dim panel As New StackPanel()
panel.Orientation = Orientation.Vertical
Dim block As New TextBlock()
block.Text = Now.ToString
panel.Children.Add(block)
Dim tip As New ToolTip()
tip.Content = panel
Return tip
End Function
Public Function ConvertBack(value As Object, targetType As Type, parameter As Object, culture As CultureInfo) As Object Implements IValueConverter.ConvertBack
Throw New NotImplementedException()
End Function
End Class
How can I call MyTooltipConveter af get a Tooltip with the current time?
Thanks
Change your MyTooltipConverter.Convert method to Return panel. Returning tip throws a System.InvalidOperationException - 'ToolTip' cannot have a logical or visual parent.
As for the binding, just using the converter works.
<ToolTip Content="{Binding Converter={StaticResource MyTooltipConverter}}" />
Of course, for the time to update you have to move the mouse around to generate a new tooltip. If what you wanted was to have a tooltip that constantly updates then you need to add a timer and update the block.Text.
Something like this:
Public Class MyTooltipConverter
Implements IValueConverter
Public Function Convert(value As Object, targetType As Type, parameter As Object, culture As CultureInfo) As Object Implements IValueConverter.Convert
If value Is Nothing Then
Return Nothing
End If
Dim panel = New StackPanel()
panel.Orientation = Orientation.Vertical
Dim block As New TextBlock()
block.Text = Now.ToString
panel.Children.Add(block)
Dim timer As New System.Windows.Threading.DispatcherTimer()
timer.Interval = New TimeSpan(0, 0, 1)
AddHandler timer.Tick, Sub()
block.Text = Now.ToString
End Sub
timer.Start()
Debug.WriteLine("Timer Started")
AddHandler panel.Unloaded, Sub(s, e)
timer.Stop()
timer = Nothing
panel = Nothing
Debug.WriteLine("Timer Stopped")
End Sub
Return panel
End Function
Public Function ConvertBack(value As Object, targetType As Type, parameter As Object, culture As CultureInfo) As Object Implements IValueConverter.ConvertBack
Throw New NotImplementedException()
End Function
End Class
I am trying to do a binding as with following data formats:
Public Structure ItemBase
Property ID As String
Property Description As String
End Structure
Namespace Classes
Public Class StockEntityClass
Implements INotifyPropertyChanged
#Region "Property Variables"
Property ID As String
Property Namee As String
Property Units As String
Property ContactID As String
Property SetCount As Integer
Property VatOnMargin As Boolean
Property Vat As Double
Property Code As String
Property _ContactNamee As String
#End Region
Public Sub New()
_IDValue = Now.ToString
_NameeValue = ""
_UnitsValue = "Pcs"
_ContactIDValue = ""
_SetCountValue = 0
_VatOnMarginValue = False
_VatValue = 14.5
_CodeValue = ""
_ContactNamee = ""
End Sub
End Class
End Namespace
In my wpf xaml window which is DataContext to Stock Entity variable, I have a combo box that is bound to BindingList(of ItemBase) and the user will select an Item and that ItemID as to get assosiated to StockEntity.ContactID.
Following is the xaml code:
<ComboBox Name="VendorsComboBox" Grid.Row="2" Grid.Column="1" VerticalAlignment="Center" DisplayMemberPath="Description" SelectedItem="{Binding Path=CustomerID}"/>
Following is Binding Code:
Dim Stock As Classes.StockEntityClass
VendorsComboBox.ItemsSource = Contacts.DBAccessFunctions.Get_ContactsByType_BaseList(DataSource, "Vendor")
StockEntityStack.DataContext = Stock
Where StockEntityStack contains the UI part which is datacontext bind to stock variable.
Could you please tell me how to write the xaml databound to it.
Try something like that:
MainWindow (XAML file):
<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">
<StackPanel>
<ComboBox ItemsSource="{Binding MySource}"
SelectedValue="{Binding ContactID}"
DisplayMemberPath="Description"
SelectedValuePath="ID"
Width="180" Height="25"
/>
<StackPanel Orientation="Horizontal" Margin="10" >
<TextBlock Text="Selected ID:" />
<TextBlock Text="{Binding ContactID}" />
</StackPanel>
</StackPanel>
</Window>
MainWindow (code-behind file):
Class MainWindow
Public Sub New()
InitializeComponent()
Me.DataContext = New MainViewModel()
End Sub
End Class
MainViewModel file:
Imports System.ComponentModel
Public Class MainViewModel
Implements INotifyPropertyChanged
#Region "Fields"
Private _selectedContactID As String = String.Empty
#End Region
#Region "Property Variables"
Property MySource As List(Of ItemBase) = New List(Of ItemBase)
Public Property ContactID As String
Get
Return _selectedContactID
End Get
Set(ByVal value As String)
_selectedContactID = value
OnPropertyChanged("ContactID")
End Set
End Property
#End Region
Public Sub New()
MySource.Add(New ItemBase(1, "test1"))
MySource.Add(New ItemBase(2, "test2"))
MySource.Add(New ItemBase(3, "test3"))
MySource.Add(New ItemBase(4, "test4"))
MySource.Add(New ItemBase(5, "test5"))
End Sub
Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
Protected Sub OnPropertyChanged(ByVal propertyName As String)
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName))
End Sub
End Class
Public Structure ItemBase
Public Sub New( _
ByVal _id As String,
ByVal _description As String
)
ID = _id
Description = _description
End Sub
Property ID As String
Property Description As String
End Structure
Can I use VB.Net's generics to simply Wpf notifiable bound controls?
Here's how to use VB.Net's Generics to create notifiable bound controls.
Class MainWindow
Public Sub New()
InitializeComponent()
Me.DataContext = Me
End Sub
Public Property NoNotify As String = "No notify"
Public Property DoesNotify As New NotifiableProperty(Of String)("DoesNotify")
Private Sub TestBtn_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles TestBtn.Click
NoNotify = "Won't Show"
DoesNotify.Value = "Notified!"
End Sub
End Class
Public Class NotifiableProperty(Of T)
Implements System.ComponentModel.INotifyPropertyChanged
Sub New()
End Sub
Sub New(ByVal InitialValue As T)
value = InitialValue
End Sub
Private m_Value As T
Public Property Value As T
Get
Return m_Value
End Get
Set(ByVal value As T)
m_Value = value
RaiseEvent PropertyChanged(Me, New System.ComponentModel.PropertyChangedEventArgs("Value"))
End Set
End Property
Private Event PropertyChanged(ByVal sender As Object, ByVal e As System.ComponentModel.PropertyChangedEventArgs) Implements System.ComponentModel.INotifyPropertyChanged.PropertyChanged
End Class
<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>
<StackPanel>
<Button Name="TestBtn" Content="Click to Test" Width="72" />
<TextBox Name="NoNotifyTB" Text="{Binding NoNotify}" />
<TextBox Name="DoesNotifyTB" Text="{Binding DoesNotify.Value}"/>
</StackPanel>
</Grid>
</Window>