我已經創建了一個GridView。我已經爲它設置了一個ItemTemplate。C#UWP GridView DataTemplate綁定問題
<GridView x:Name="gvMain"
HorizontalAlignment="Stretch"
HorizontalContentAlignment="Stretch"
ItemTemplate="{StaticResource GridTemplate}"
AllowDrop="True"
CanDragItems="True"
CanReorderItems="True"
ScrollViewer.VerticalScrollBarVisibility="{Binding gridViewCarsScrollViewVisible, ElementName=carsControl}">
<DataTemplate x:Key="GridTemplate" x:DataType="models:Car">
<Grid
HorizontalAlignment="Stretch"
Background="Transparent"
Tapped="CarTapped">
<Border x:Name="borderBackground"
Background="White"
Opacity="{x:Bind IsSelected, Converter={StaticResource TransparencyConverter}, Mode=OneWay}"/>
</Grid>
</DataTemplate>
在我的模型我有一個屬性:
private bool isSelected;
public bool IsSelected
{
get { return isSelected; }
set { isSelected = value; OnPropertyChanged(); }
}
而且我改變它:
private void CarTapped(object sender, Windows.UI.Xaml.Input.TappedRoutedEventArgs e)
{
var grid = sender as Grid;
var car = grid.DataContext as Car;
car.IsSelected = !car.IsSelected;
}
有,例如300輛在GridView控件。問題是,當我開始向下滾動時,無論上升,下降,綁定都將顯示給每個將顯示的汽車的IsSelected屬性,如果我返回已經「加載」的汽車仍然綁定正在尋找屬性值。如果有更多的屬性,如果綁定設置在某個控件上,它將會轉到其中的每一個屬性。這使得滾動變得不可能,因爲這是非常不連貫的。
爲什麼綁定試圖在項目滾動到視圖中時獲取值? 我試圖使用綁定,而不是x:bind,我試圖設置模式:一種方式,兩種方式,一次...我試圖設置UpdateSourceTrigger = PropertyChanged ...但每次都是相同的結果。
任何線索我在這裏做錯了什麼?
你能發佈一個截圖/ GIF究竟發生了什麼?這有助於更好地理解問題 – AVK