简体   繁体   中英

Select tabItem programmatically when selecting an element in silverlight

I have an image editor which has a number of image elements and text elements on a canvas. I have two separate tabs which allow the user to re-size the text or image. I'd like to switch to the text tab when I select a text element and switch to the image tab when I select an image. I'm having trouble figuring out how to do it.

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 (not my MainPage code-behind) -

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

    }

  }
}

How do I get it to switch tabs here? I'm sure it's probably something simple, I just can't put my finger on it. The values seem to be passed to the bindings just fine when I debug but they still aren't switching right

Your properties (TabTextSel and TabImgSel) should be of type bool, not string. And you should set them to true of false (bool).

您必须分配给属性而不是变量,以便可以通过调用NotifyPropertyChanged方法来进行数据绑定。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM