如何在不按下按钮的情况下通过 C# 代码在内容控件之间切换?
How to switch between content controls via c# code, without button pressing?
我有一个 C# 应用程序(wpf、mvvm)。我有 3 个单选按钮,描述 3 个垂直选项卡,每个选项卡都有一个命令 (ICommand) 来显示内容控件(视图),这也在 XAML 中说明。在 c# 代码中,控件之间的切换是通过使一个 view.Visibility = 可见,而其他 - 隐藏来完成的。单击选项卡时一切正常。
但是,我现在想要在特定的不活动超时后 - 切换到 "Home" 内容控件,但没有运气。计时器正确超时,并且在 timerElapsed 上,我获得了使此主页内容控件可见的功能,以及隐藏其他控件的功能(就像我 select 此选项卡时所做的那样),但内容控件不会更改.
`<StackPanel Orientation="Vertical" Grid.Row="1" Margin="0,85,0,0">
<RadioButton Content="{l:Translation HomePage}"
IsChecked="{Binding IsHomeMode, Mode=OneWay, FallbackValue=True}"
Style="{StaticResource LeftNavigation_ToggleButtonStyle}"
Command="{Binding ShowHomePageCommand}"/>
<RadioButton Content="{l:Translation PatientList}"
Style="{StaticResource LeftNavigation_ToggleButtonStyle}"
Command="{Binding ShowAllCustomersCommand}"/>
<RadioButton Content="{l:Translation ResumeSession}"
Style="{StaticResource LeftNavigation_ToggleButtonStyle}"
Command="{Binding ShowAllSessionsCommand}"/>
</StackPanel>`
<Grid>
<ContentControl Content="{Binding SessionsHistoryView}"/>
<ContentControl Content="{Binding HomepageView}"/>
</Grid>
绑定到视图意味着您的 ViewModel 包含 属性 并且它不是 MVVM 方式。还有更好的方法:
在您的父 ViewModel
中定义 CurrentPage 属性。
private object _currentPage;
public object CurrentPage
{
get { return _currentPage; }
set
{
_currentPage = value;
OnPropertyChanged(nameof(CurrentPage));
}
}
只绑定一个内容控件到它
<ContentControl Content="{Binding CurrentPage}"/>
为ResourceDictionary
中的每个页面ViewModel
创建一个DataTemplate
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication1">
<DataTemplate DataType="{x:Type local:HomePageViewModel}">
<!--View definition goes here-->
</DataTemplate>
</ResourceDictionary>
在应用程序的字典或任何您需要的地方ResourceDictionary
注册您的
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Views.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
在您的命令实现中将必需的 ViewModel
设置为 CurrentPage
属性。您可以在此处添加超时或任何其他逻辑。更喜欢异步执行命令。
我有一个 C# 应用程序(wpf、mvvm)。我有 3 个单选按钮,描述 3 个垂直选项卡,每个选项卡都有一个命令 (ICommand) 来显示内容控件(视图),这也在 XAML 中说明。在 c# 代码中,控件之间的切换是通过使一个 view.Visibility = 可见,而其他 - 隐藏来完成的。单击选项卡时一切正常。 但是,我现在想要在特定的不活动超时后 - 切换到 "Home" 内容控件,但没有运气。计时器正确超时,并且在 timerElapsed 上,我获得了使此主页内容控件可见的功能,以及隐藏其他控件的功能(就像我 select 此选项卡时所做的那样),但内容控件不会更改.
`<StackPanel Orientation="Vertical" Grid.Row="1" Margin="0,85,0,0">
<RadioButton Content="{l:Translation HomePage}"
IsChecked="{Binding IsHomeMode, Mode=OneWay, FallbackValue=True}"
Style="{StaticResource LeftNavigation_ToggleButtonStyle}"
Command="{Binding ShowHomePageCommand}"/>
<RadioButton Content="{l:Translation PatientList}"
Style="{StaticResource LeftNavigation_ToggleButtonStyle}"
Command="{Binding ShowAllCustomersCommand}"/>
<RadioButton Content="{l:Translation ResumeSession}"
Style="{StaticResource LeftNavigation_ToggleButtonStyle}"
Command="{Binding ShowAllSessionsCommand}"/>
</StackPanel>`
<Grid>
<ContentControl Content="{Binding SessionsHistoryView}"/>
<ContentControl Content="{Binding HomepageView}"/>
</Grid>
绑定到视图意味着您的 ViewModel 包含 属性 并且它不是 MVVM 方式。还有更好的方法:
在您的父
ViewModel
中定义 CurrentPage 属性。private object _currentPage; public object CurrentPage { get { return _currentPage; } set { _currentPage = value; OnPropertyChanged(nameof(CurrentPage)); } }
只绑定一个内容控件到它
<ContentControl Content="{Binding CurrentPage}"/>
为
中的每个页面ResourceDictionary
ViewModel
创建一个DataTemplate
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:WpfApplication1"> <DataTemplate DataType="{x:Type local:HomePageViewModel}"> <!--View definition goes here--> </DataTemplate> </ResourceDictionary>
在应用程序的字典或任何您需要的地方
ResourceDictionary
注册您的<Application.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="Views.xaml"/> </ResourceDictionary.MergedDictionaries> </ResourceDictionary> </Application.Resources>
在您的命令实现中将必需的
ViewModel
设置为CurrentPage
属性。您可以在此处添加超时或任何其他逻辑。更喜欢异步执行命令。