在silverlight中选择元素时,以编程方式选择tabItem

时间:2012-08-07 10:33:37

标签: c# wpf silverlight tabitem

我有一个图像编辑器,它在画布上有许多图像元素和文本元素。我有两个单独的选项卡,允许用户重新调整文本或图像的大小。我选择文本元素时切换到文本选项卡,选择图像时切换到图像选项卡。我无法弄清楚如何去做。

MainPage.xaml -

<!-- Control Panel -->
<sdk:TabControl x:Name="elementEditor" Margin="10,0,10,19" Width="215" Background="#FFD6D6D6"
       Grid.RowSpan="2">//removed selectedIndex binding from here
<sdk:TabItem x:Name="tabCanvasSettings" DataContext="{Binding Project}">
    <sdk:TabItem.Header x:Name="tabCanvas">
        <TextBlock Text="Canvas" />
    </sdk:TabItem.Header>
    <sdk:TabItem.Content>
        <!-- Content -->
    </sdk:TabItem.Content>
</sdk:TabItem>

<!-- Text tab -->
<sdk:TabItem x:Name="tabText" IsSelected="{Binding Path=TabTextSel}">
    <sdk:TabItem.Header>
        <TextBlock Text="Text" />
    </sdk:TabItem.Header>
    <sdk:TabItem.Content>
        <!-- Content -->
    </sdk:TabItem.Content>
</sdk:TabItem>

<!-- Image tab -->
<sdk:TabItem x:Name="tabImage" IsSelected="{Binding Path=TabImgSel}">
    <sdk:TabItem.Header>
        <TextBlock Text="Image" />
    </sdk:TabItem.Header>
    <sdk:TabItem.Content>
        <!-- Content -->
    </sdk:TabItem.Content>
</sdk:TabItem>
</sdk:TabControl>

Project.cs(不是我的MainPage代码隐藏) -

using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Effects;
using System.Windows.Media.Imaging;
using ImageEditor.Client.Behaviors;
using ImageEditor.Client.Views;
using System.Linq;
using ImageEditor.Client.ImageDesignerService;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
namespace ImageEditor.Client.BLL
{
  public class Project : INotifyPropertyChanged
  {
    private bool tabTextSel;
    public bool TabTextSel
    {
        get { return tabTextSel; }
        set
        {
            tabTextSel = value;
            NotifyPropertyChanged("TabTextSel");
        }
    }

    private bool tabImgSel;
    public bool TabImgSel
    {
        get { return tabImgSel; }
        set
        {
            tabImgSel = value;
            NotifyPropertyChanged("TabImgSel");
        }
    }
    //Other properties and methods

    public void element_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
    {
        this.SelectedElement = sender as FrameworkElement;
        if (sender is TextBlock)
        {
            this.SelectedTextElement = sender as TextBlock;

            //Change tab to text tab
            tabImgSel = false;
            tabTextSel = true;
        }
        else if (sender is Image)
        {
            this.SelectedImageElement = sender as Image;

            //Change tab to image tab
            tabTextSel = false;
            tabImgSel = true;
        }

    }

  }
}

如何在此处切换标签?我敢肯定它可能很简单,我不能把手指放在上面。当我调试时,值似乎传递给绑定就好了,但它们仍然没有正确切换

2 个答案:

答案 0 :(得分:1)

您的属性(TabTextSel和TabImgSel)应为bool类型,而不是字符串。你应该将它们设置为true(bool)。

答案 1 :(得分:0)

您必须为属性分配而不是变量,因此通过调用NotifyPropertyChanged方法可以使数据绑定工作....