我有问题。我希望在ListView中选择项目之后在stacklayout中显示标签中的更多信息,但我不能使用x:堆栈名称,因为在xamarin后面的代码中看不到我的引用。我这样做了
代码:
private void ChallengeList_ItemSelected(object sender, SelectedItemChangedEventArgs e)
{
if (e.SelectedItem == null)
return;
More.IsEnabled = true;
//ChallengeList.SelectedItem = null;
}
我的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" IsEnabled="False" x:Name="More" >
<Label Text="sdfghjkhgfdsfghjkljhgfdsadfghjkljhgfdsaSDFGHJKJHGFDSAsdfghjkhgfds" TextColor="#ff3f50" FontSize="17" FontAttributes="Bold" HorizontalOptions="Center"
LineBreakMode="WordWrap"/>
</StackLayout>
</StackLayout>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
答案 0 :(得分:0)
您无法看到元素More
,因为它位于ListView的项目模板中,因此它是动态生成的。您可以访问More
内的ViewCell
,但不能访问ItemSource
以外的内容(在您的网页/视图中)。
让我们假设您在后面的代码中将ChallengeList的IsEnabled
设置为您的视图模型列表。
我建议您将属性IsEnabled添加到视图模型中,将StackLayout中的ChallengeList_ItemSelected
绑定到此属性以及视图模型的public class MyViewModel : ViewModelBase
{
private bool _isEnabled = false;
public bool IsEnabled
{
get => _isEnabled;
set => Set( ref _isEnabled, value);
}
}
<StackLayout HorizontalOptions="CenterAndExpand" IsEnabled="{Binding IsEnabled}" x:Name="More" >
...
</StackLayout>
private void ChallengeList_ItemSelected(object sender, SelectedItemChangedEventArgs e)
{
if (e.SelectedItem is MyViewModel viewModel)
{
viewModel.IsEnabled = true;
}
}
更改属性
示例:
{{1}}