绑定在 Mvvm wpf 和 controltab 中不起作用

Binding not working in Mvvm wpf and controltab

您好,错误 是第一次初始化我的选项卡绑定工作,但是当我想更改我的 tabHeader 的值时,绑定不起作用。

当我选择了一个选项卡后,我想显示按钮并隐藏所有未选择的选项卡按钮,

Acceuil.xaml

<TabControl TabStripPlacement="Top" ItemsSource="{Binding Tabs, Mode=TwoWay}" x:Name="ParentTab" SelectedItem="{Binding SelectedTab, Mode=TwoWay}">
   <TabControl.ItemTemplate>
      <DataTemplate>
         <StackPanel Width="Auto" Orientation="Horizontal">
            <TextBox Text="{Binding Guid, Mode=TwoWay}" Height="1" Width="1" Visibility="Hidden"></TextBox>
            <Image Source="{Binding HeaderImg,  Mode=TwoWay}" Height="20" Width="20"/>
            <Label Content="{Binding HeaderSrt, Mode=TwoWay}" Height="Auto" Width="Auto"/>
            <Button Visibility="{Binding IsEnable, Mode=TwoWay}" Content="X" Height="Auto" Width="Auto" Command="{Binding DataContext.Cmd_CloseTab,ElementName=ParentTab}"/>
         </StackPanel>
      </DataTemplate>
   </TabControl.ItemTemplate>
   <TabControl.ContentTemplate>
      <DataTemplate>
         <ContentControl Content="{Binding Content}" />
      </DataTemplate>
   </TabControl.ContentTemplate>
</TabControl>

在AccueilViewModel.cs

public sealed class TabItem{
   public string HeaderImg { get; set; }
   public string HeaderSrt { get; set; }
   public string Guid { get; set; }
   public Visibility IsEnable { get; set; }
   public ViewModelBase Content { get; set; }
}

public class AccueilViewModel : ViewModelBase{
   private TabItem m_SelectedTab;
   private ObservableCollection<TabItem> m_Tabs;
   public ICommand Cmd_CloseTab { get; set; }

   public ObservableCollection<TabItem> Tabs{
      get{ return m_Tabs; }
      set{ m_Tabs = value; RaisePropertyChanged("Tabs"); }
   }

   public TabItem SelectedTab{
      get{ return m_SelectedTab; }
      set{
         m_SelectedTab = value;
         m_SelectedTab.IsEnable = Visibility.Visible;
         ActivateBtnClose();  // The binding don't work
         RaisePropertyChanged("SelectedTab");
         RaisePropertyChanged("Tabs");
         }
      }

   public AccueilViewModel(){
      Cmd_CloseTab = new RelayCommand(() => Execute_Command_CloseTab());
      m_Tabs = new ObservableCollection<TabItem>();
      Tabs.Add(new TabItem {
               Guid = "System",
               HeaderImg = SingletonIcone.Instance.IcoLoupe,
               IsEnable = Visibility.Visible,
               HeaderSrt = "",
               Content = new ContactTabsViewModel() });
      Tabs.Add(new TabItem {
               Guid = "System",
               HeaderImg = SingletonIcone.Instance.IcoConfig,
               IsEnable = Visibility.Visible,
               HeaderSrt = "",
               Content = new ConfigViewModel() });

         ActivateBtnClose(); // Do Work for binding
      }

我想在未选中选项卡时隐藏按钮:

          private void ActivateBtnClose() {

             foreach (var item in Tabs){
                item.IsEnable = Visibility.Hidden;
                if (m_SelectedTab != null) { 
                   if ( m_SelectedTab.Guid != "System" && m_SelectedTab.Guid == item.Guid){
                      m_SelectedTab.IsEnable = Visibility.Visible;
                      item.IsEnable = Visibility.Visible;
                   }
                }

             }
          }

          private void Execute_Command_CloseTab(){
             Tabs[0].HeaderSrt = "AZE"; //The binding don't work
             Console.WriteLine("lol");
                //Tabs.Remove(SelectedTab);
             Console.WriteLine("lol");
                RaisePropertyChanged("Tabs");
            }

          private bool IsTabOppen(string sGuid) {
             bool bRet = false;
             foreach (var item in Tabs){
                if (item.Guid == sGuid) {
                   bRet = true;
                   SelectedTab = item;
                }
             }
             return bRet;
          }
   }

感谢您的帮助

TabItem更改为:

public sealed class TabItem : ViewModelBase
{
    private string _headerImg;
    private string _headerSrt { get; set; }
    private string _guid { get; set; }
    private Visibility _isEnable { get; set; }
    private ViewModelBase _content { get; set; }

    public string HeaderImg
    {
        get { return _headerImg; }
        set
        {
            _headerImg = value;
            RaisePropertyChanged("HeaderImg");
        }
    }

    // Set the properties for the other fields as above
}

你好,谢谢你它正在工作

答案是:

public sealed class TabItem: ViewModelBase
{

    public string HeaderImg { get; set; }
    public string HeaderSrt { get; set; }
    public string Guid { get; set; }

    //public bool IsEnable { get; set; }
    public ViewModelBase Content { get; set; }

    private Visibility _MessageVisibilty;
    public Visibility MessageVisibilty
    {
        get { return _MessageVisibilty; }
        set {
            _MessageVisibilty = value;
            RaisePropertyChanged("MessageVisibilty");}
    }
}