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

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


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


主页.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>

在 HomeViewModel.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");

         }

      }



陪伴而非守候
浏览 86回答 2
2回答

HUX布斯

更改TabItem为:public sealed class TabItem : ViewModelBase{&nbsp; &nbsp; private string _headerImg;&nbsp; &nbsp; private string _headerSrt { get; set; }&nbsp; &nbsp; private string _guid { get; set; }&nbsp; &nbsp; private Visibility _isEnable { get; set; }&nbsp; &nbsp; private ViewModelBase _content { get; set; }&nbsp; &nbsp; public string HeaderImg&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; get { return _headerImg; }&nbsp; &nbsp; &nbsp; &nbsp; set&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _headerImg = value;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; RaisePropertyChanged("HeaderImg");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; // Set the properties for the other fields as above}

慕姐4208626

答案是:public sealed class TabItem: ViewModelBase{&nbsp; &nbsp; public string HeaderImg { get; set; }&nbsp; &nbsp; public string HeaderSrt { get; set; }&nbsp; &nbsp; public string Guid { get; set; }&nbsp; &nbsp; //public bool IsEnable { get; set; }&nbsp; &nbsp; public ViewModelBase Content { get; set; }&nbsp; &nbsp; private Visibility _MessageVisibilty;&nbsp; &nbsp; public Visibility MessageVisibilty&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; get { return _MessageVisibilty; }&nbsp; &nbsp; &nbsp; &nbsp; set {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _MessageVisibilty = value;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; RaisePropertyChanged("MessageVisibilty");}&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP