Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example for SetCurrentValue #1

Merged
merged 1 commit into from
Feb 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions TabTest/MyControl.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@
<TextBlock Text="{Binding SelectedTabIndex}"></TextBlock>
</TextBlock>
<Button Content="Change Tab" Command="{Binding ChangeTabCommand}" Width="150" Height="40"/>
<Button Content="Change Tab from Within Control" Width="200" Height="40" Click="Button_Click"/>
</StackPanel>
</UserControl>
47 changes: 7 additions & 40 deletions TabTest/MyControl.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ namespace TabTest
/// <summary>
/// Interaction logic for MyControl.xaml
/// </summary>
public partial class MyControl : UserControl, INotifyPropertyChanged
public partial class MyControl : UserControl /* controls don't implement INotifyPropertyChanged, use DependencyProperty instead */
{

public MyControlViewModel ViewModel
Expand All @@ -30,7 +30,7 @@ public MyControlViewModel ViewModel
}

public static readonly DependencyProperty ViewModelProperty =
DependencyProperty.Register("ViewModel", typeof(MyControlViewModel), typeof(MyControl), new PropertyMetadata(default(MyControlViewModel), ViewModelChanged));
DependencyProperty.Register("ViewModel", typeof(MyControlViewModel), typeof(MyControl), new PropertyMetadata(default(MyControlViewModel)));


public int SelectedTabIndex
Expand All @@ -40,53 +40,20 @@ public int SelectedTabIndex
}

public static readonly DependencyProperty SelectedTabIndexProperty =
DependencyProperty.Register("SelectedTabIndex", typeof(int), typeof(MyControl), new FrameworkPropertyMetadata(0, SelectedTabIndexChanged) { BindsTwoWayByDefault=true });
DependencyProperty.Register("SelectedTabIndex", typeof(int), typeof(MyControl), new FrameworkPropertyMetadata(0) { BindsTwoWayByDefault=true });


public MyControl()
{
InitializeComponent();
}

private static void ViewModelChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
MyControl thisControl = d as MyControl;
thisControl.RaisePropertyChanged(nameof(thisControl.ViewModel));

if (thisControl.ViewModel != null)
{
//Binding b = new Binding();
//b.Source = thisControl.ViewModel;
//b.Path = new PropertyPath("SelectedTabIndex");
//b.Mode = BindingMode.TwoWay;
//b.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
//BindingOperations.SetBinding(thisControl, SelectedTabIndexProperty, b);

thisControl.SelectedTabIndex = thisControl.ViewModel.SelectedTabIndex;
}
}

private static void SelectedTabIndexChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
MyControl thisControl = d as MyControl;
thisControl.RaisePropertyChanged(nameof(thisControl.SelectedTabIndex));

int newvalue = (int)e.NewValue; // diagnostic
// setting the Source to operate on the control, otherwise we'd operate on the DataContext
SetBinding(SelectedTabIndexProperty, new Binding("ViewModel.SelectedTabIndex") { Source = this });
}


#region INotifyPropertyChanged implementation
public event PropertyChangedEventHandler PropertyChanged;
public void RaisePropertyChanged([CallerMemberNameAttribute] string propertyName = "") => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));

public void SetProp<T>(ref T prop, T value, [CallerMemberNameAttribute] string propertyName = "")
private void Button_Click(object sender, RoutedEventArgs e)
{
if (!Object.Equals(prop, value))
{
prop = value;
RaisePropertyChanged(propertyName);
}
SetCurrentValue(SelectedTabIndexProperty, SelectedTabIndex == 0 ? 1 : 0);
}
#endregion
}
}