S
spzh
Guest
I have a following code:
1) Visual Basic. MainPage.xaml.vb
Imports System.Collections.Generic
Imports System.Globalization
Imports Windows.UI
Public Class TabMatConv1
Implements IValueConverter
Public Function Convert(value As Object, targetType As Type, parameter As Object, language As String) As Object Implements IValueConverter.Convert
Return value.ToString
End Function
Public Function ConvertBack(value As Object, targetType As Type, parameter As Object, language As String) As Object Implements IValueConverter.ConvertBack
Throw New NotImplementedException()
End Function
End Class
Public Class InttoSrtConv
' конвертор для конвертирования целочисленных значений в строковые и обратно
Implements IValueConverter
Public Function Convert(value As Object, targetType As Type, parameter As Object, language As String) As Object Implements IValueConverter.Convert
Return value.ToString
End Function
Public Function ConvertBack(value As Object, targetType As Type, parameter As Object, language As String) As Object Implements IValueConverter.ConvertBack
Try
Dim current1 As NumberFormatInfo = NumberFormatInfo.CurrentInfo
Dim StringValue As String = value.ToString
StringValue = StringValue.Replace(".", current1.CurrencyDecimalSeparator)
StringValue = StringValue.Replace(",", current1.CurrencyDecimalSeparator)
Select Case targetType.Name
Case "Single"
Return System.Convert.ToSingle(StringValue, current1)
Case "Double"
Return System.Convert.ToDouble(StringValue, current1)
Case "UInt32"
Return System.Convert.ToUInt32(StringValue, current1)
Case "Int32"
Return System.Convert.ToInt32(StringValue, current1)
Case "Integer"
Return CType(StringValue, Integer)
Case "UInteger"
Return CType(StringValue, UInteger)
End Select
Catch ex As Exception
Return 0
End Try
End Function
End Class
Public NotInheritable Class MainPage
Inherits Page
Public Class Matirial
Public Property CodeMat As UShort
Public Property NameMat As String
Public Sub New(ByVal CodeMat1 As UShort,
ByVal NameMat1 As String)
CodeMat = CodeMat1
NameMat = NameMat1
End Sub
End Class
Function GetMaterial() As IEnumerable(Of Matirial)
Return New ObservableCollection(Of Matirial) From
{
New Matirial(1, "New Steel"),
New Matirial(2, "New Iron"),
New Matirial(3, "Old Steel&Iron"),
New Matirial(4, "Asbestos"),
New Matirial(5, "Сoncrete"),
New Matirial(6, "Plastic")
}
End Function
Public Class Pipe_Type
Public Property Label As String
Public Property Lx As UInt16
Public Property Diam As UInt16
Public Property Mat As UInt16
Public Sub New(ByVal Label1 As String, Lx1 As UInt16, Diam1 As UInt16, mat1 As UInt16)
Label = Label1
Lx = Lx1
Diam = Diam1
Mat = mat1
End Sub
End Class
Public TabMat As ObservableCollection(Of Matirial) = GetMaterial()
Public Pipes As New ObservableCollection(Of Pipe_Type)()
Dim curMatView = From Mat In TabMat Select Mat.CodeMat, Mat.NameMat
Public CV As CollectionViewSource = New CollectionViewSource With {.Source = curMatView}
Private MainGrid As Grid
Dim lvp As ListView
Dim cvs As CollectionViewSource
Sub New()
InitializeComponent()
Pipes.Add(New Pipe_Type("Label1", 122, 100, 1))
Pipes.Add(New Pipe_Type("Label2", 152, 150, 2))
Pipes.Add(New Pipe_Type("Label3", 162, 200, 2))
Pipes.Add(New Pipe_Type("Label4", 171, 50, 1))
Pipes.Add(New Pipe_Type("Label5", 181, 60, 4))
MainGrid = New Grid With {.BorderThickness = New Thickness(5),
.BorderBrush = New SolidColorBrush(Windows.UI.Colors.Blue)} ' ,
' .Visibility = Visibility.Visible, .CenterPoint = New Numerics.Vector3(0)}
Dim GridCol As New ColumnDefinition With {.Width = New GridLength(1, GridUnitType.Auto)}
MainGrid.ColumnDefinitions.Add(GridCol)
MainGrid.ColumnDefinitions.Add(New ColumnDefinition With {.Width = New GridLength(1, GridUnitType.Star)})
MainGrid.ColumnDefinitions.Add(New ColumnDefinition With {.Width = New GridLength(1, GridUnitType.Auto)})
MainGrid.RowDefinitions.Add(New RowDefinition With {.Height = New GridLength(1, GridUnitType.Auto)})
MainGrid.RowDefinitions.Add(New RowDefinition With {.Height = New GridLength(3, GridUnitType.Star)})
Me.Content = MainGrid
Dim grd As Grid = New Grid
grd.ColumnDefinitions.Add(New ColumnDefinition With {.Width = New GridLength(1, GridUnitType.Auto)})
grd.RowDefinitions.Add(New RowDefinition With {.Height = New GridLength(3, GridUnitType.Auto)})
grd.RowDefinitions.Add(New RowDefinition With {.Height = New GridLength(3, GridUnitType.Star)})
grd.RowDefinitions.Add(New RowDefinition With {.Height = New GridLength(3, GridUnitType.Auto)})
grd.RowDefinitions.Add(New RowDefinition With {.Height = New GridLength(3, GridUnitType.Auto)})
grd.RowDefinitions.Add(New RowDefinition With {.Height = New GridLength(3, GridUnitType.Star)})
MakeChildAndSetCR(grd, 2, 1)
lvp = New ListView With {.Name = "LV2",
.HorizontalAlignment = HorizontalAlignment.Left,
.IsSynchronizedWithCurrentItem = False,
.VerticalAlignment = VerticalAlignment.Stretch,
.RequestedTheme = ElementTheme.Default,
.SelectionMode = ListViewSelectionMode.Single,
.ItemTemplate = CType(Me.Resources("dt2"), DataTemplate),
.ReorderMode = ListViewReorderMode.Enabled
}
Dim StPnllvP As StackPanel = New StackPanel With {.Orientation = Orientation.Horizontal, .Margin = New Thickness(2)}
Dim borderLVP = New Border With {.BorderBrush = New SolidColorBrush(Colors.Green),
.BorderThickness = New Thickness(2),
.Background = New SolidColorBrush(Colors.White), .CornerRadius = New CornerRadius(20),
.Child = StPnllvP}
StPnllvP.Children.Add(lvp)
Dim GreatStPanel1 = New StackPanel With {.Orientation = Orientation.Horizontal, .Margin = New Thickness(2)}
GreatStPanel1.Children.Add(borderLVP)
grd.Children.Add(GreatStPanel1)
Grid.SetColumn(GreatStPanel1, 0)
Grid.SetRow(GreatStPanel1, 4)
Dim bng2 As Binding = New Binding With {.Mode = BindingMode.TwoWay, .Source = Pipes}
lvp.SetBinding(ListView.ItemsSourceProperty, bng2)
End Sub
Sub MakeChildAndSetCR(obj As Object, c As UInt16, r As UInt16)
MainGrid.Children.Add(obj)
Grid.SetColumn(obj, c)
Grid.SetRow(obj, r)
End Sub
End Class
2. XAML. MainPage.xaml.
<Page x:Name="Page"
x:Class="ComboTest.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:ComboTest"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" >
<Page.Resources>
<local:InttoSrtConv x:Name="Conv2"></local:InttoSrtConv>
<local:TabMatConv1 x:Name="Conv3"></local:TabMatConv1>
<CollectionViewSource x:Name="MatCollection" Source="{x:Bind TabMat, Mode=OneWay}"/>
<!--
-->
<DataTemplate x:Key ="dt2">
<StackPanel Orientation="Horizontal" Margin="2" BorderBrush="Black" BorderThickness="2">
<TextBox Name="Label" Text="{Binding Path=Label, Mode=TwoWay}" Margin="2" Width="90" TextAlignment="Center" InputScope="Number" MaxLength ="7"
AcceptsReturn ="False" />
<TextBox Name ="Lx" Text="{Binding Path=Lx, Mode=TwoWay, Converter={StaticResource Conv2 }}" Margin="2" Width="90" TextAlignment="Center" InputScope="Number" MaxLength ="7"
AcceptsReturn ="False"/>
<TextBox Name ="Diam" Text="{Binding Diam, Mode=TwoWay, Converter={StaticResource Conv2 }}" Margin="2" Width="90" TextAlignment="Center"
InputScope="Number" MaxLength ="5" AcceptsReturn ="False"/>
<ComboBox Name ="CB_PipeType"
SelectedItem ="{Binding Path=Mat,Mode=TwoWay}"
SelectedValuePath="CodeMat"
DisplayMemberPath="CodeMat"
Margin="2"
Width="90"
IsEditable ="True"
IsDropDownOpen="False"
ItemsSource="{Binding Source={StaticResource MatCollection},Mode=OneWay}" >
</ComboBox>
</StackPanel>
</DataTemplate>
</Page.Resources>
</Page>
When I make first binding for combobox,
SelectedItem ="{Binding Path=Mat,Mode=TwoWay}"
Code works normally, each combobox element show correct start value, but have a null value in ItemsSource.
Then I add Binding for Combobox in MainPage.xml,
ItemsSource="{Binding Source={StaticResource MatCollection},Mode=OneWay}"
any changing of value combobox element in runtime, change values in all combobox items.
I think, Combobox have a two binding, and XAML use ItemsSource Combobox binding for generate ListView.
If I create Combobox items manually, in XAML code, like this:
<x:String>1</x:String>
<x:String>2</x:String>
It's work fine.
Have anybody positive experience for use Combobox in Datatemplate for ListView ItemSource and it binding ItemsSource and SelectedItem properties?
Continue reading...
1) Visual Basic. MainPage.xaml.vb
Imports System.Collections.Generic
Imports System.Globalization
Imports Windows.UI
Public Class TabMatConv1
Implements IValueConverter
Public Function Convert(value As Object, targetType As Type, parameter As Object, language As String) As Object Implements IValueConverter.Convert
Return value.ToString
End Function
Public Function ConvertBack(value As Object, targetType As Type, parameter As Object, language As String) As Object Implements IValueConverter.ConvertBack
Throw New NotImplementedException()
End Function
End Class
Public Class InttoSrtConv
' конвертор для конвертирования целочисленных значений в строковые и обратно
Implements IValueConverter
Public Function Convert(value As Object, targetType As Type, parameter As Object, language As String) As Object Implements IValueConverter.Convert
Return value.ToString
End Function
Public Function ConvertBack(value As Object, targetType As Type, parameter As Object, language As String) As Object Implements IValueConverter.ConvertBack
Try
Dim current1 As NumberFormatInfo = NumberFormatInfo.CurrentInfo
Dim StringValue As String = value.ToString
StringValue = StringValue.Replace(".", current1.CurrencyDecimalSeparator)
StringValue = StringValue.Replace(",", current1.CurrencyDecimalSeparator)
Select Case targetType.Name
Case "Single"
Return System.Convert.ToSingle(StringValue, current1)
Case "Double"
Return System.Convert.ToDouble(StringValue, current1)
Case "UInt32"
Return System.Convert.ToUInt32(StringValue, current1)
Case "Int32"
Return System.Convert.ToInt32(StringValue, current1)
Case "Integer"
Return CType(StringValue, Integer)
Case "UInteger"
Return CType(StringValue, UInteger)
End Select
Catch ex As Exception
Return 0
End Try
End Function
End Class
Public NotInheritable Class MainPage
Inherits Page
Public Class Matirial
Public Property CodeMat As UShort
Public Property NameMat As String
Public Sub New(ByVal CodeMat1 As UShort,
ByVal NameMat1 As String)
CodeMat = CodeMat1
NameMat = NameMat1
End Sub
End Class
Function GetMaterial() As IEnumerable(Of Matirial)
Return New ObservableCollection(Of Matirial) From
{
New Matirial(1, "New Steel"),
New Matirial(2, "New Iron"),
New Matirial(3, "Old Steel&Iron"),
New Matirial(4, "Asbestos"),
New Matirial(5, "Сoncrete"),
New Matirial(6, "Plastic")
}
End Function
Public Class Pipe_Type
Public Property Label As String
Public Property Lx As UInt16
Public Property Diam As UInt16
Public Property Mat As UInt16
Public Sub New(ByVal Label1 As String, Lx1 As UInt16, Diam1 As UInt16, mat1 As UInt16)
Label = Label1
Lx = Lx1
Diam = Diam1
Mat = mat1
End Sub
End Class
Public TabMat As ObservableCollection(Of Matirial) = GetMaterial()
Public Pipes As New ObservableCollection(Of Pipe_Type)()
Dim curMatView = From Mat In TabMat Select Mat.CodeMat, Mat.NameMat
Public CV As CollectionViewSource = New CollectionViewSource With {.Source = curMatView}
Private MainGrid As Grid
Dim lvp As ListView
Dim cvs As CollectionViewSource
Sub New()
InitializeComponent()
Pipes.Add(New Pipe_Type("Label1", 122, 100, 1))
Pipes.Add(New Pipe_Type("Label2", 152, 150, 2))
Pipes.Add(New Pipe_Type("Label3", 162, 200, 2))
Pipes.Add(New Pipe_Type("Label4", 171, 50, 1))
Pipes.Add(New Pipe_Type("Label5", 181, 60, 4))
MainGrid = New Grid With {.BorderThickness = New Thickness(5),
.BorderBrush = New SolidColorBrush(Windows.UI.Colors.Blue)} ' ,
' .Visibility = Visibility.Visible, .CenterPoint = New Numerics.Vector3(0)}
Dim GridCol As New ColumnDefinition With {.Width = New GridLength(1, GridUnitType.Auto)}
MainGrid.ColumnDefinitions.Add(GridCol)
MainGrid.ColumnDefinitions.Add(New ColumnDefinition With {.Width = New GridLength(1, GridUnitType.Star)})
MainGrid.ColumnDefinitions.Add(New ColumnDefinition With {.Width = New GridLength(1, GridUnitType.Auto)})
MainGrid.RowDefinitions.Add(New RowDefinition With {.Height = New GridLength(1, GridUnitType.Auto)})
MainGrid.RowDefinitions.Add(New RowDefinition With {.Height = New GridLength(3, GridUnitType.Star)})
Me.Content = MainGrid
Dim grd As Grid = New Grid
grd.ColumnDefinitions.Add(New ColumnDefinition With {.Width = New GridLength(1, GridUnitType.Auto)})
grd.RowDefinitions.Add(New RowDefinition With {.Height = New GridLength(3, GridUnitType.Auto)})
grd.RowDefinitions.Add(New RowDefinition With {.Height = New GridLength(3, GridUnitType.Star)})
grd.RowDefinitions.Add(New RowDefinition With {.Height = New GridLength(3, GridUnitType.Auto)})
grd.RowDefinitions.Add(New RowDefinition With {.Height = New GridLength(3, GridUnitType.Auto)})
grd.RowDefinitions.Add(New RowDefinition With {.Height = New GridLength(3, GridUnitType.Star)})
MakeChildAndSetCR(grd, 2, 1)
lvp = New ListView With {.Name = "LV2",
.HorizontalAlignment = HorizontalAlignment.Left,
.IsSynchronizedWithCurrentItem = False,
.VerticalAlignment = VerticalAlignment.Stretch,
.RequestedTheme = ElementTheme.Default,
.SelectionMode = ListViewSelectionMode.Single,
.ItemTemplate = CType(Me.Resources("dt2"), DataTemplate),
.ReorderMode = ListViewReorderMode.Enabled
}
Dim StPnllvP As StackPanel = New StackPanel With {.Orientation = Orientation.Horizontal, .Margin = New Thickness(2)}
Dim borderLVP = New Border With {.BorderBrush = New SolidColorBrush(Colors.Green),
.BorderThickness = New Thickness(2),
.Background = New SolidColorBrush(Colors.White), .CornerRadius = New CornerRadius(20),
.Child = StPnllvP}
StPnllvP.Children.Add(lvp)
Dim GreatStPanel1 = New StackPanel With {.Orientation = Orientation.Horizontal, .Margin = New Thickness(2)}
GreatStPanel1.Children.Add(borderLVP)
grd.Children.Add(GreatStPanel1)
Grid.SetColumn(GreatStPanel1, 0)
Grid.SetRow(GreatStPanel1, 4)
Dim bng2 As Binding = New Binding With {.Mode = BindingMode.TwoWay, .Source = Pipes}
lvp.SetBinding(ListView.ItemsSourceProperty, bng2)
End Sub
Sub MakeChildAndSetCR(obj As Object, c As UInt16, r As UInt16)
MainGrid.Children.Add(obj)
Grid.SetColumn(obj, c)
Grid.SetRow(obj, r)
End Sub
End Class
2. XAML. MainPage.xaml.
<Page x:Name="Page"
x:Class="ComboTest.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:ComboTest"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" >
<Page.Resources>
<local:InttoSrtConv x:Name="Conv2"></local:InttoSrtConv>
<local:TabMatConv1 x:Name="Conv3"></local:TabMatConv1>
<CollectionViewSource x:Name="MatCollection" Source="{x:Bind TabMat, Mode=OneWay}"/>
<!--
-->
<DataTemplate x:Key ="dt2">
<StackPanel Orientation="Horizontal" Margin="2" BorderBrush="Black" BorderThickness="2">
<TextBox Name="Label" Text="{Binding Path=Label, Mode=TwoWay}" Margin="2" Width="90" TextAlignment="Center" InputScope="Number" MaxLength ="7"
AcceptsReturn ="False" />
<TextBox Name ="Lx" Text="{Binding Path=Lx, Mode=TwoWay, Converter={StaticResource Conv2 }}" Margin="2" Width="90" TextAlignment="Center" InputScope="Number" MaxLength ="7"
AcceptsReturn ="False"/>
<TextBox Name ="Diam" Text="{Binding Diam, Mode=TwoWay, Converter={StaticResource Conv2 }}" Margin="2" Width="90" TextAlignment="Center"
InputScope="Number" MaxLength ="5" AcceptsReturn ="False"/>
<ComboBox Name ="CB_PipeType"
SelectedItem ="{Binding Path=Mat,Mode=TwoWay}"
SelectedValuePath="CodeMat"
DisplayMemberPath="CodeMat"
Margin="2"
Width="90"
IsEditable ="True"
IsDropDownOpen="False"
ItemsSource="{Binding Source={StaticResource MatCollection},Mode=OneWay}" >
</ComboBox>
</StackPanel>
</DataTemplate>
</Page.Resources>
</Page>
When I make first binding for combobox,
SelectedItem ="{Binding Path=Mat,Mode=TwoWay}"
Code works normally, each combobox element show correct start value, but have a null value in ItemsSource.
Then I add Binding for Combobox in MainPage.xml,
ItemsSource="{Binding Source={StaticResource MatCollection},Mode=OneWay}"
any changing of value combobox element in runtime, change values in all combobox items.
I think, Combobox have a two binding, and XAML use ItemsSource Combobox binding for generate ListView.
If I create Combobox items manually, in XAML code, like this:
<x:String>1</x:String>
<x:String>2</x:String>
It's work fine.
Have anybody positive experience for use Combobox in Datatemplate for ListView ItemSource and it binding ItemsSource and SelectedItem properties?
Continue reading...