MainWindow.xaml:
<Grid DataContext="{StaticResource vm}" >
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="50"/>
</Grid.RowDefinitions>
<TabControl SelectedIndex="{Binding Selected,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Grid.Row="0" x:Name="TestTabs">
<TabItem Name="Tab1" Header="News" />
<TabItem Name="Tab2" Header="DLC" />
<TabItem Name="Tab3" Header="Settings"/>
</TabControl>
<DockPanel x:Name="rp" Grid.Row="0" LastChildFill="False" HorizontalAlignment="Stretch">
<Canvas DockPanel.Dock="Left" >
<Rectangle x:Name="Rect1" Fill="#ff0000" VerticalAlignment="Top" Height="4" Margin="0,25,0,0"
SnapsToDevicePixels="True" UseLayoutRounding="True" RenderOptions.EdgeMode="Aliased"
Width="{Binding ElementName=TestTabs, Path=SelectedItem.ActualWidth}" RenderOptions.BitmapScalingMode="HighQuality" />
</Canvas>
</DockPanel>
<StackPanel Orientation="Horizontal" Grid.Row="1">
<Label Content="selected" Width="100" Height="40" />
<TextBox x:Name="sele" Text="{Binding Selected}" Width="100" Height="40" Background="AliceBlue" Margin="5"/>
<Label Content="previousSelected" Width="100" Height="40"/>
<TextBox x:Name="prev" Text="{Binding PreviousSelected,Mode=OneWay}" Width="100" Height="40" Background="AliceBlue" Margin="5"/>
</StackPanel>
</Grid>
MainWindow.xaml.cs:
using System.ComponentModel;
using System.Windows;
namespace TabControlBindSlide
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
}
class TestViewModel : INotifyPropertyChanged
{
private int _selected;
public int Selected
{
get { return _selected; }
set
{
int temp = _selected;
_selected = value;
_previousSelected = temp;
NotifyPropertyChanged("Selected", temp, value);
NotifyPropertyChanged("PreviousSelected", temp, temp);
}
}
int _previousSelected = 0;
public int PreviousSelected
{
get { return _previousSelected; }
}
public event PropertyChangedEventHandler PropertyChanged;
protected void NotifyPropertyChanged<T>(string propertyName, T oldvalue, T newvalue)
{
OnPropertyChanged(this, new PropertyChangedExtendedEventArgs<T>(propertyName, oldvalue, newvalue));
}
public virtual void OnPropertyChanged(object sender, PropertyChangedEventArgs e)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
handler(sender, e);
}
}
public class PropertyChangedExtendedEventArgs<T> : PropertyChangedEventArgs
{
public virtual T OldValue { get; private set; }
public virtual T NewValue { get; private set; }
public PropertyChangedExtendedEventArgs(string propertyName, T oldValue, T newValue)
: base(propertyName)
{
OldValue = oldValue;
NewValue = newValue;
}
}
}
MainWindow.xaml:(include resource) :
192830-btxt.txt
If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.