【问题标题】:Binding in ItemContainerStyle works, binding in DataTemplate does not - how to fix?ItemContainerStyle 中的绑定有效,DataTemplate 中的绑定无效 - 如何修复?
【发布时间】:2011-12-24 18:28:49
【问题描述】:

我有一个自定义选项卡控件,它在 TabItem 内显示一个框架和一个自定义标题。在 ItemContainerStyle 中设置绑定以显示 TabItem 有效,但我无法在 DataTemplate 中获取绑定以将标题文本设置为正常工作。请注意,TabControl 绑定到项的 ObservableCollection,其中包含两个属性 - TI,要在 TabItem 中显示的框架,和 DisplayName,要在标题中显示的字符串。

===============XAML=================

<TabControl Height="Auto" Name="mainTabControl" Width="Auto" IsSynchronizedWithCurrentItem="True" Margin="4" >
  <TabControl.ItemTemplate>
    <DataTemplate>
      <DockPanel Width="Auto">
        <Button 
            Content="X"
            Cursor="Hand"
            DockPanel.Dock="Right"
            Focusable="False"
            FontFamily="Courier" 
            FontSize="9"
            FontWeight="Bold"  
            Margin="0,1,0,0"
            Padding="0"
            VerticalContentAlignment="Bottom"
            Width="16" Height="16"               
            >
          <!-- Command=""      //Command binding TBD later
            CommandParameter="" -->
        </Button>
        <ContentPresenter 
            Content ="{Binding DisplayName}"
            VerticalAlignment="Center" 
            Margin="0,0,7,0"
          />
      </DockPanel>
    </DataTemplate>
  </TabControl.ItemTemplate>

  <TabControl.Style>
    <Style TargetType="TabControl">
      <Style.Triggers>
        <DataTrigger Binding="{Binding Items.Count, RelativeSource={RelativeSource Self}}"
                        Value="0">
          <Setter Property="Visibility" Value="Hidden" />
        </DataTrigger>
      </Style.Triggers>
    </Style>
  </TabControl.Style>
  <TabControl.ItemContainerStyle  >
    <Style TargetType="TabItem">
      <Setter Property="Content" Value="{Binding TI}"/>
    </Style>
  </TabControl.ItemContainerStyle>
</TabControl>

============绑定项目=============

  class TabItemContainer : Frame
  {
    public event DependencyPropertyChangedEventHandler PropertyChanged;

    private void NotifyPropertyChanged(String info)
    {
      if (PropertyChanged != null)
      {
        PropertyChanged(this, new DependencyPropertyChangedEventArgs());
      }
    }

    private string displayname;

    public string DisplayName
    {
      get { return displayname; }
      set
      {
        if (value != this.displayname)
        {
          displayname = value;
          NotifyPropertyChanged("DisplayName");
        }
      }
    }

    public Frame TI { get; set; }
  }

===========代码隐藏=============

ObservableCollection<TabItemContainer> Tablist;

public MainWindow()
{
  InitializeComponent();

  Tablist = new ObservableCollection<TabItemContainer>();

  TabItemContainer tic = new TabItemContainer();
  tic.DataContext = tic;
  tic.DisplayName = "Untitled";
  tic.TI = new Frame();
  tic.TI.Navigate(new Page1());
  Tablist.Add(tic);
  mainTabControl.ItemsSource = Tablist;
  mainTabControl.SelectedIndex = Tablist.Count - 1;
}

【问题讨论】:

  • “不起作用”是什么意思?你有错误吗?什么都没有出现?据我所知,在标题中显示DisplayName 的绑定看起来是正确的,应该可以工作。
  • 没有错误,也没有任何显示。它应该在选项卡标题中显示“无标题”。
  • 你至少看到你的关闭按钮了吗?
  • 是的,关闭按钮出现。如果我将 ContentPresenter、Content 替换为文本,例如“hello”,则 hello 会显示在选项卡标题中。

标签: wpf binding tabs


【解决方案1】:

Header 绑定添加到您的 TabItem 样式

<TabControl.ItemContainerStyle>
    <Style TargetType="TabItem">
        <Setter Property="Content" Value="{Binding TI}"/>
        <Setter Property="Header" Value="{Binding }" />
    </Style>
</TabControl.ItemContainerStyle>

您的 Header 中的 DataContext 不会自动设置为您的 TabItemContainer(我认为是这样的......一定是错误的)

【讨论】:

    猜你喜欢
    • 2015-02-18
    • 1970-01-01
    • 2012-02-11
    • 2012-08-05
    • 2016-07-19
    • 2015-09-17
    • 2012-03-17
    • 2013-05-29
    • 2011-12-09
    相关资源
    最近更新 更多