使用按钮绑定选项卡项

时间:2015-10-14 11:32:14

标签: c# wpf

我有一个带有多个标签的wpf。该应用程序是学生的一步一步学习工具。我希望最初锁定标签。当用户在选项卡中输入了正确的数据时,按下继续按钮后会启用下一个数据。

我尝试绑定tabitem的'isEnabled'属性,并在View模型中按下按钮时将其设置为true。它不起作用。

以下是我的代码的一些片段: XAML:

<TabItem Header="Step 1"><my:Step1 Loaded="Step1_Loaded" /></TabItem>
 <TabItem Header="Step 2"><my:Step2 Loaded="Step2_Loaded" /></TabItem>
 <TabItem IsEnabled="{Binding step3Enabled, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Header="Step 3"><my:Step3 Loaded="Step3_Loaded" /></TabItem>

按钮的代码隐藏:

private void proceed2_Click(object sender, RoutedEventArgs e)
        {
            var vm = DataContext as ViewModel.ValuesCheck;
            if (vm != null)
               vm.Step2check();
           
        }

查看型号:

class ViewModel:INotifyPropertyChanged
    {
        bool _step3enabled = false;
        public bool step3Enabled
        {
            get { return _step3enabled; }
            set { _step3enabled = value; OnPropertyChanged("step3Enabled"); }
        }
        public void Step2check()
        {
             this.step3Enabled = true;
        }

        public event PropertyChangedEventHandler PropertyChanged;

        private void OnPropertyChanged(string propertyName)
        { 
            if(PropertyChanged != null)
            {
                PropertyChanged(this,new PropertyChangedEventArgs(propertyName));
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

据我所知,您需要一些导航控件,根据某些条件显示其内容。我建议您使用列表框控件并管理其ListBoxItems内容可见性或类似的内容。

  1. 的Xaml:

    <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>
    

  2. 视图模型

        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;
    
    }
    
  3. 模型

        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();
        }
    }
    
  4. 运行时查看: enter image description here

  5. 你必须模板化和设计所有这些东西......

  6. 此致