gpt4 book ai didi

mvvm - 如何在 MVVM 中应用 View 端过滤?

转载 作者:行者123 更新时间:2023-12-03 10:48:44 24 4
gpt4 key购买 nike

我在我的项目中使用 Telerik RadGridView(它本质上是一个标准的 GridView)。
这个组件有它自己的过滤功能,我想利用它。

我计划根据几个组合框选择的值进行过滤。因此,如果我对 MVVM 有正确的认识,我需要将组合绑定(bind)到一些 ViewModel 的属性。但是这里有一个问题,如何将这些选定的值传递回 View 的组件?如何使其在选定值更改时刷新过滤?

upd:我使用 SimpleMVVM 框架。

主窗口的 XAML:

<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
......
DataContext="{Binding Source={StaticResource Locator}, Path=MainPageViewModel}">

<StackPanel Height="auto">
<telerik:RadMenu VerticalAlignment="Top">
......
</telerik:RadMenu>
<my:Expander VerticalAlignment="Top" HorizontalAlignment="Stretch" Height="auto" Width="auto"/>
<my:CustomerView Margin="0,0,0,0" VerticalAlignment="Top" HorizontalAlignment="Stretch" Height="auto" Width="auto"/>
</StackPanel>

扩展器的 XAML:
<UserControl xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
...... >


<Grid Margin="10,10,0,10" Width="684" Height="97" VerticalAlignment="Top" HorizontalAlignment="Left" >

<telerik:RadExpander x:Name="radExpander" IsExpanded="True" HorizontalAlignment="Stretch" VerticalAlignment="Top" telerik:AnimationManager.IsAnimationEnabled="True" Margin="0,0,0,0" Grid.RowSpan="2">
<telerik:RadComboBox HorizontalAlignment="Left" Margin="244,-2,0,0" VerticalAlignment="Top" Width="154" Height="26"
ItemsSource="{Binding Path=AllLevels}" DisplayMemberPath="name" SelectedItem="{Binding SelectedEventLevel}"/>
.......

</Grid>
</telerik:RadExpander>
</Grid>

CustomerView 的 XAML:
       <telerik1:RadGridView Name="EventList" .... ItemsSource="{Binding SportEventsList}" AutoGenerateColumns="False">
<telerik1:RadGridView.Columns>
.....
</telerik1:RadGridView.Columns>
</telerik1:RadGridView>

Viewmodel 的代码片段:
      private ObservableCollection<sportevent> _sportEventsList;
public ObservableCollection<sportevent> SportEventsList
{
get { return _sportEventsList; }
set
{
_sportEventsList = value;
NotifyPropertyChanged(vm => vm.SportEventsList);
}
}

最佳答案

将以下属性添加到您的 VM:

    private ObservableCollection<yourType> allLevels;
public ObservableCollection<yourType> AllLevels
{
get
{
return allLevels;
}
set
{
allLevels = value;
RaisePropertyChanged("AllLevels");
}
}

private yourType selectedEventLevel;
public yourTypeSelectedEventLevel
{
get
{
return selectedEventLevel;
}
set
{
selectedEventLevel = value;
RaisePropertyChanged("SelectedEventLevel");
}
}

我假设 MainPageViewModel继承 SimpleViewModelBase

关于mvvm - 如何在 MVVM 中应用 View 端过滤?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15675392/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com