C# ListView中所选行的双倍高度
阿罗哈 我的XAML代码中有一个ListView,里面有一个网格 现在有一段来自我同事的代码,我需要完成。 目标是将选定行的高度增加一倍。下面的截图是第一行高度的两倍,但不是选定的C# ListView中所选行的双倍高度,c#,xaml,xamarin,C#,Xaml,Xamarin,阿罗哈 我的XAML代码中有一个ListView,里面有一个网格 现在有一段来自我同事的代码,我需要完成。 目标是将选定行的高度增加一倍。下面的截图是第一行高度的两倍,但不是选定的 <Grid.Style> <Style TargetType="Grid"> <Setter Property="HeightRequest" Value="60"/> <Style.Triggers> <DataTrigger TargetT
<Grid.Style>
<Style TargetType="Grid">
<Setter Property="HeightRequest" Value="60"/>
<Style.Triggers>
<DataTrigger TargetType="Grid" Binding="{Binding Selected}" Value="True">
<Setter Property="HeightRequest" Value="120"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Grid.Style>
我必须改变什么,选定的对象就是那个具有双倍高度的对象?我正在与xamarin/xamarin forms UWP/Android合作
附加信息:我必须在MVVM模式下工作。所以我应该避免使用传统事件 如果要在运行时更改ViewCell的高度。您可以调用
cell.ForceUpdateSize()
以更新单元格的大小
因此,我们使用TriggerAction
来更改此值,并告诉ViewCell更新其大小
public class ForceUpdateSizeTriggerAction : TriggerAction<VisualElement>
{
public int HeighRequest { set; get; }
public ForceUpdateSizeTriggerAction() : base()
{
}
protected override void Invoke(VisualElement sender)
{
var parent = sender.Parent;
while (parent != null && !(parent is ViewCell))
{
parent = parent.Parent;
}
if (parent is ViewCell cell)
{
Device.BeginInvokeOnMainThread(() =>
{
sender.HeightRequest = HeighRequest;
cell.ForceUpdateSize();
});
}
}
}
<ListView HasUnevenRows="True" ItemsSource="{Binding MyItems}" SelectedItem="{Binding SelectItems,Mode=TwoWay}">
<Grid.Style>
<Style TargetType="Grid">
<Setter Property="HeightRequest" Value="60"/>
<Style.Triggers>
<DataTrigger TargetType="Grid" Binding="{Binding IsCheck}" Value="True">
<DataTrigger.EnterActions>
<local:ForceUpdateSizeTriggerAction HeighRequest="120" />
</DataTrigger.EnterActions>
<DataTrigger.ExitActions>
<local:ForceUpdateSizeTriggerAction HeighRequest="60" />
</DataTrigger.ExitActions>
</DataTrigger>
</Style.Triggers>
</Style>
</Grid.Style>
public class MyViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void NotifyPropertyChanged([CallerMemberName] string propertyName = "")
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
public ObservableCollection<Model> MyItems { get; set; }
private Model selectItems;
public Model SelectItems
{
get
{
return selectItems;
}
set
{
if (selectItems != value)
{
selectItems = value;
NotifyPropertyChanged();
foreach(Model model in MyItems)
{
if(selectItems==model)
{
model.IsCheck = !model.IsCheck;
}
}
}
}
}
public MyViewModel()
{
SelectItems = null;
MyItems = new ObservableCollection<Model>() {
new Model(){ IsCheck=false},
new Model(){ IsCheck=false},
new Model(){ IsCheck=false},
new Model(){ IsCheck=false},
new Model(){ IsCheck=false},
new Model(){ IsCheck=false},
new Model(){ IsCheck=false},
};
}
public class Model : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void NotifyPropertyChanged([CallerMemberName] string propertyName = "")
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
private bool isCheck;
public bool IsCheck
{
get
{
return isCheck;
}
set
{
if (isCheck != value)
{
isCheck = value;
NotifyPropertyChanged();
}
}
}
private double height;
public double Height
{
get
{
return height;
}
set
{
if (height != value)
{
height = value;
NotifyPropertyChanged();
}
}
}
}