这适用于VB.NET,而XAML适用于UWP。我在DataTemplate中使用ListView、MyList:
<ListView x:Name="MyList">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Button Content="{Binding button1.Content}"></Button>
<TextBlock Text="{Binding col2}"></TextBlock>
<Border Child="{Binding button2}"></Border>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
我有:DataList As New cObservableListBase(Of Item)
,还有
Public Class Item
'List Properties
Public Property button1 As CustomButton
Public Property col2 As String
Public Property button2 As CustomButton
End Class
Public Class CustomButton
Inherits Button
Implements IComparable
Public Function CompareTo(obj As Object) As Integer Implements IComparable.CompareTo
CompareTo = 0
If Content > CType(obj, CustomButton).Content Then
CompareTo = 1
ElseIf Content < CType(obj, CustomButton).Content Then
CompareTo = -1
End If
End Function
End Class
和
MyList.ItemsSource = DataList
For i = 65 To 85
Dim b1 As New CustomButton With {.Content = Convert.ToChar(i)}
Dim b2 As New CustomButton With {.Content = Convert.ToChar(i)}
DataList.Add(New Item With {.button1 = b1, .col2 = Convert.ToChar(i), .button2 = b2})
Next
ListView包含3列,当我在任何一列上排序DataList时,绑定在文本上的前2列将被正确排序,但绑定在控件本身上的第三列将显示不同的结果。注意: DataList本身对这3列进行了正确的排序,但列表视图中的问题是不能正确地显示第3列。
是我做错了什么呢,还是这是个bug?以及如何正确地绑定到控件?
编辑:
我觉得我必须添加一个小程序,以使任何人想要尝试它都很容易。
<StackPanel Orientation="Vertical">
<Button Name="ADD" Content="Add" Click="AddClick" />
<Button Name="Order" Content="Order" Click="OrderClick"/>
<ListView x:Name="MyList">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Button Content="{Binding button1.Content}"></Button>
<TextBlock Text="{Binding col2}"></TextBlock>
<Border Child="{Binding button2}"></Border>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackPanel>
在代码后面:
Public NotInheritable Class MainPage
Inherits Page
Public DataList As New ObservableCollection(Of Item)
Public Sub AddClick()
MyList.ItemsSource = DataList
For i = 65 To 85
Dim b1 As New CustomButton With {.Content = Convert.ToChar(i)}
Dim b2 As New CustomButton With {.Content = Convert.ToChar(i)}
DataList.Add(New Item With {.button1 = b1, .col2 = Convert.ToChar(i), .button2 = b2})
Next
End Sub
Public Sub OrderClick()
Static NextOrder As Boolean = False
Dim Sorted As IOrderedEnumerable(Of Item)
If NextOrder Then
Sorted = DataList.OrderBy(Function(p) p.col2)
Else
Sorted = DataList.OrderByDescending(Function(p) p.col2)
End If
NextOrder = Not NextOrder
Dim SortedList = Sorted.ToList
DataList.Clear()
For i = 0 To SortedList.Count - 1
DataList.Add(SortedList(i))
Next
End Sub
End Class
Public Class Item
'List Properties
Public Property button1 As CustomButton
Public Property col2 As String
Public Property button2 As CustomButton 'or UIElement
End Class
Public Class CustomButton
Inherits Button
Implements IComparable
Public Function CompareTo(obj As Object) As Integer Implements IComparable.CompareTo
Return Content.ToString.CompareTo(CType(obj, CustomButton).Content.ToString)
End Function
End Class
首先添加列表,然后尝试使用排序按钮对其进行排序,问题位于第3列,它的排序不正确。但在对程序进行模拟时,我发现DataList的顺序是正确的,即使在第三列中也是如此,但问题出在列表视图中,没有正确显示!!
发布于 2018-05-28 14:08:01
我不能在我这边重现你的问题,我检查了你的代码。我发现您的数据绑定不是一个好的实践,我重写了您的模型类和xamlto,使它们看起来简洁明了。请参考以下内容。
public class Item
{
public string Content { get; set; }
public string Color { get; set; }
public UIElement BTNElement { get; set; }
}
XAML
<ListView Name="MyList">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel>
<Button Content="{Binding Content}"></Button>
<TextBlock Text="{Binding Color}"></TextBlock>
<Border Child="{Binding BTNElement}"></Border>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
使用
MyList.ItemsSource = new List<Item> {
new Item { Content = "ClickMe", Color = "Red", BTNElement = new Button { Content = "Nico" } },
new Item { Content = "ClickMe", Color = "Green", BTNElement = new Button { Content = "Kite" } },
new Item { Content = "ClickMe", Color = "Blue", BTNElement = new Button { Content = "Chuchu" } } };
https://stackoverflow.com/questions/50506174
复制相似问题