我有取消选择项目的问题。我希望再次点击stacklayout IsVisible = false;但我不知道为什么我的代码再次点击后无法正常工作。 StackLayout仍然可见
private bool _isVisible;
public new bool IsVisible
{
get { return _isVisible; }
set { _isVisible = value; OnPropertyChanged(nameof(IsVisible)); }
}
private void ChallengeList_ItemSelected(object sender, SelectedItemChangedEventArgs e)
{
if (e.SelectedItem == null)
return;
int licz = 0;
if (e.SelectedItem is MainPage viewModel && licz == 0)
{
int li = 0;
li++;
viewModel.IsVisible = true;
if (li == 2)
{
viewModel.IsVisible = false;
licz = 0;
}
}
}
的Xaml:
<ListView x:Name="ChallengeList" SeparatorColor="#3d122c" HasUnevenRows="True"
ItemSelected="ChallengeList_ItemSelected" RelativeLayout.YConstraint="{ConstraintExpression ElementName=Lab, Constant=0,Factor=1,Property=Height,Type=RelativeToView}"
RelativeLayout.HeightConstraint="{ConstraintExpression Property=Height,Factor=0.8,Type=RelativeToParent}">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout Orientation="Horizontal" BackgroundColor="#40FFFFFF" Padding="10">
<StackLayout HorizontalOptions="CenterAndExpand">
<Label Text="{Binding Title}" TextColor="#ff3f50" FontSize="17" FontAttributes="Bold" HorizontalOptions="Center"/>
<StackLayout HorizontalOptions="CenterAndExpand" IsVisible="{Binding IsVisible}" x:Name="More" Padding="5">
<Label Text="sdfghjkhgfdsfghjkljhgfdsadfghjkljhgfdsaSDFGHJKJHGFDSAsdfghjkhgfds" TextColor="#ff3f50" FontSize="17" FontAttributes="Bold" HorizontalOptions="Center"
LineBreakMode="WordWrap"/>
</StackLayout>
</StackLayout>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
答案 0 :(得分:1)
由于e.SelectedItem
的类型不是MainPage
,因此它始终为空。
ViewModel
:在ListView
项目中添加一个属性,以指示该项目当前是否已被选中:
public class ItemViewModel : BaseViewModel
{
public string Title { get; set; }
private bool isVisible;
public bool IsVisible
{
get { return isVisible; }
set { SetField(ref isVisible, value); }
}
}
BaseViewModel :
public class BaseViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
protected bool SetField<T>(ref T field, T value, [CallerMemberName] string propertyName = null)
{
if (Object.Equals(field, value)) return false;
field = value;
OnPropertyChanged(propertyName);
return true;
}
}
然后在页面的ViewModel
中,您可以在SelectedItem
事件中设置IsSelected
的{{1}}值:
XAML:
SelectedItem
在<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:App7"
x:Class="App7.MainPage">
<ContentPage.BindingContext>
<local:MainViewModel/>
</ContentPage.BindingContext>
<ListView x:Name="ChallengeList"
SeparatorColor="#3d122c"
HasUnevenRows="True"
ItemsSource="{Binding Title}"
SelectedItem="{Binding SelectedItem}"
RelativeLayout.YConstraint="{ConstraintExpression ElementName=Lab, Constant=0,Factor=1,Property=Height,Type=RelativeToView}"
RelativeLayout.HeightConstraint="{ConstraintExpression Property=Height,Factor=0.8,Type=RelativeToParent}">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout
Orientation="Horizontal"
BackgroundColor="#40FFFFFF"
Padding="10">
<StackLayout HorizontalOptions="CenterAndExpand">
<Label Text="{Binding Title}"
TextColor="#ff3f50"
FontSize="17"
FontAttributes="Bold"
HorizontalOptions="Center"/>
<StackLayout
HorizontalOptions="CenterAndExpand"
IsVisible="{Binding IsVisible}"
x:Name="More"
Padding="5">
<Label
Text="QWER"
TextColor="#ff3f50"
FontSize="17"
FontAttributes="Bold"
HorizontalOptions="Center"
LineBreakMode="WordWrap"/>
</StackLayout>
</StackLayout>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</ContentPage>
:
MainViewModel
答案 1 :(得分:1)
尝试删除变量li和licz,然后切换IsVisible值,如下所示:
private void ChallengeList_ItemSelected(object sender, SelectedItemChangedEventArgs e)
{
if (e.SelectedItem == null)
return;
viewModel.IsVisible = !viewModel.IsVisible;
}