C# 使用按钮跨选项卡项绑定
我有一个有多个标签页的wpf。该应用程序是学生逐步学习的工具。我希望最初锁定选项卡。当用户在选项卡中输入正确的数据时,按下“继续”按钮后,将启用下一个 我尝试绑定tabitem的“isEnabled”属性,并在视图模型中按下按钮时将其设置为true。它不起作用 以下是我的一些代码片段: XAML:C# 使用按钮跨选项卡项绑定,c#,wpf,C#,Wpf,我有一个有多个标签页的wpf。该应用程序是学生逐步学习的工具。我希望最初锁定选项卡。当用户在选项卡中输入正确的数据时,按下“继续”按钮后,将启用下一个 我尝试绑定tabitem的“isEnabled”属性,并在视图模型中按下按钮时将其设置为true。它不起作用 以下是我的一些代码片段: XAML: 据我所知,您需要一些导航控件,根据某些条件显示其内容。我可以建议您使用listbox控件并管理其ListBoxItems内容可见性或类似的内容 Xaml: <Grid> <Grid
据我所知,您需要一些导航控件,根据某些条件显示其内容。我可以建议您使用listbox控件并管理其ListBoxItems内容可见性或类似的内容
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<ListBox Grid.Row="0" ItemsSource="{Binding Controls}" SelectedItem="{Binding CurrentControlContent}"
VerticalContentAlignment="Stretch"
HorizontalContentAlignment="Stretch">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"></StackPanel>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate>
<Border Margin="0" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Visibility="{Binding Path = TabIsEnabled, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource Converter}}"
BorderBrush="Tomato" BorderThickness="1" IsEnabled="{Binding Path = TabIsEnabled, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}">
<Button Width="100" Height="30" IsEnabled="{Binding Path = TabIsEnabled, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
IsHitTestVisible="False" Content="{Binding Path = Content, UpdateSourceTrigger=PropertyChanged}"></Button>
</Border>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<Grid Grid.Row="1">
<Grid.RowDefinitions>
<RowDefinition Height="*"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
</Grid.RowDefinitions>
<ContentControl Content="{Binding CurrentControlContent.Content}"></ContentControl>
<Button Grid.Row="1" Content="Check" Command="{Binding CheckCommand}"></Button>
</Grid>
您是否有绑定错误?不清楚您是否确实创建了
ViewModel
的实例,并将其设置为DataContext
。讨论设计:考虑使用MVVM,每个选项卡是一个单独的视图模型,您可以创建一些进度控制来显示问题总数和当前问题,并简单地使用<代码>内容控制< /代码>显示当前问题。
private TabAndControlModel _selectedTabControlModel;
private ICommand _checkCommand;
private TabAndControlModel _currentControlContent;
public ObservableCollection<TabAndControlModel> Controls { get; set; }
public TabAndControlModel CurrentControlContent
{
get { return _currentControlContent; }
set
{
_currentControlContent = value;
OnPropertyChanged();
}
}
public ICommand CheckCommand
{
get { return _checkCommand ?? (_checkCommand = new RelayCommand(Check)); }
}
private void Check()
{
var index = Controls.IndexOf(CurrentControlContent);
var nextIndex = index + 1;
if(nextIndex >= Controls.Count) return;
CurrentControlContent = Controls[nextIndex];
CurrentControlContent.TabIsEnabled = true;
}
private bool _tabIsEnabled;
private string _content;
public bool TabIsEnabled
{
get { return _tabIsEnabled; }
set
{
_tabIsEnabled = value;
OnPropertyChanged();
}
}
public string Content
{
get { return _content; }
set
{
_content = value;
OnPropertyChanged();
}
}