[英]Maui Filter Content based on button, update UI
I'm trying to filter the content based on some buttons like this我正在尝试根据这样的一些按钮来过滤内容
I have the XAML setup as this我有这样的 XAML 设置
<CollectionView Grid.Row="0"
ItemsSource="{Binding Categories}"
ItemsLayout="HorizontalList"
Margin="20,0"
x:Name="els">
<CollectionView.ItemTemplate>
<DataTemplate x:DataType="x:String">
<Button Style="{StaticResource ButtonOutline}" Text="{Binding .}" FontSize="20" Margin="5,10"
Clicked="CategorySelected"/>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
And my Page has the following method for the event, the idea is to change the style of the button that was clicked我的页面有以下事件方法,这个想法是改变被点击按钮的样式
private void CategorySelected(object sender, EventArgs e)
{
var btn = (Button)sender;
btn.TextColor = Color.Parse("#FFFFFF");
viewModel.ChangeCategory(btn.Text);
}
Everything is working as expected, but I can't seem to find a way to change the style of all the other buttons that weren't clicked.一切都按预期工作,但我似乎无法找到一种方法来更改所有其他未单击的按钮的样式。 How can I have access to them?我怎样才能访问它们? I have access to the CollectionView in my code, but I'm not able to access the Children variable that I can in debug mode (after going to base multiple times), I've tried all types of casting, but no success我可以在我的代码中访问 CollectionView,但我无法访问我在调试模式下可以访问的 Children 变量(在多次访问 base 之后),我尝试了所有类型的转换,但没有成功
Is there a way to reach those children?有没有办法接触到这些孩子? Or maybe another solution that is more MVVM like或者也许是另一个更像 MVVM 的解决方案
Styling on its own is enough to achieve this, for example by setting the VisualStates.样式本身就足以实现这一点,例如通过设置 VisualStates。
You are trying to make a RadioGroup with RadioButtons.您正在尝试使用 RadioButtons 创建一个 RadioGroup。 You can just use those controls.您可以只使用这些控件。 (But I get the idea that you are practicing, I recognize this code) (不过我看你是在练习,我认得这段代码)
DataTriggers can be used to change the appearance of your controls. DataTriggers 可用于更改控件的外观。 Combined with some Value Convertors you can get this job done.结合一些值转换器,您可以完成这项工作。
I would recommend also using SelectedItem of CollectionView.我还建议使用 CollectionView 的 SelectedItem。 However, right now there are some pretty serious bugs regarding horizontal CollectionView.然而,现在有一些关于水平 CollectionView 的非常严重的错误。 (Forget about spacing) As well as bugs about disabling/enabling and changing the visual state. (忘记间距)以及有关禁用/启用和更改视觉 state 的错误。
My code is full with "//TODO: Fix when github issue link is resolved."我的代码充满了“//TODO:修复github 问题链接已解决”。 So I do not advise you to put too much effort right now.所以我不建议你现在付出太多的努力。 Make it tappable and move on.使其可点击并继续。
Edit: About your question, check this for example:编辑:关于你的问题,例如检查这个:
<RadioButton Content="{Binding .}">
<RadioButton.ControlTemplate>
<ControlTemplate>
<Grid RowDefinitions="30,4">
<Label Text="{TemplateBinding Content}" />
<BoxView Grid.Row="1" Color="Transparent"/>
</Grid>
</ControlTemplate>
</RadioButton.ControlTemplate>
And to make it change style:并使其改变风格:
<ControlTemplate>
<Grid RowDefinitions="30,4">
<VisualStateManager.VisualStateGroups>
<VisualStateGroupList>
<VisualStateGroup x:Name="CheckedStates">
<VisualState x:Name="Checked">
<VisualState.Setters>
<Setter
TargetName="TextLabel"
Property="Label.TextColor"
Value="{StaticResource Primary}"/>
<Setter
TargetName="Indicator"
Property="BoxView.Color"
Value="{StaticResource Primary}"/>
</VisualState.Setters>
</VisualState>
<VisualState x:Name="Unchecked">
<VisualState.Setters>
<Setter
TargetName="TextLabel"
Property="Label.TextColor"
Value="White"/>
<Setter
TargetName="Indicator"
Property="BoxView.Color"
Value="Transparent"/>
</VisualState.Setters>
</VisualState>
</VisualStateGroup>
</VisualStateGroupList>
</VisualStateManager.VisualStateGroups>
<Label Text="{TemplateBinding Content}" x:Name="TextLabel" />
<BoxView x:Name="Indicator" Grid.Row="1" Color="Transparent"/>
</Grid>
This is from here:这是从这里:
https://dev.to/davidortinau/making-a-tabbar-or-segmentedcontrol-in.net-maui-54ha https://dev.to/davidortinau/making-a-tabbar-or-segmentedcontrol-in.net-maui-54ha
Extremely useful example about making your own controls.关于制作您自己的控件的非常有用的示例。 I recommend going over everything in that blog.我建议查看该博客中的所有内容。
If someone has the same issue, I would advise to checkout this project on Github, he solves this perfectly maui-starbucks-ui如果有人有同样的问题,我建议在 Github 上查看这个项目,他完美地解决了这个maui-starbucks-ui
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.