numをバインドさせつつ、別のViewModelをDataContextとして設定しているので動いていないのです。numプロパティをバインドさせてそれで終わりにするのが普通です。
XAML
1<Window x:Class="WpfApplication2.MainWindow"
2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4 xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
5 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
6 xmlns:local="clr-namespace:WpfApplication2"
7 mc:Ignorable="d"
8 Title="MainWindow" Height="350" Width="525">
9 <Window.DataContext>
10 <local:MainWindowViewModel />
11 </Window.DataContext>
12 <Grid>
13 <local:UserControl1 prop="{Binding num}" />
14 </Grid>
15</Window>
XAML
1<UserControl x:Class="WpfApplication2.UserControl1"
2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
5 xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
6 xmlns:local="clr-namespace:WpfApplication2"
7 mc:Ignorable="d" Name="usercontrol"
8 d:DesignHeight="300" d:DesignWidth="300">
9 <Grid>
10 <TextBox Text="{Binding prop, ElementName=usercontrol}" />
11 </Grid>
12</UserControl>
C#
1public partial class UserControl1 : UserControl
2{
3 public UserControl1()
4 {
5 InitializeComponent();
6 }
7
8 public int prop
9 {
10 get { return (int)GetValue(propProperty); }
11 set { SetValue(propProperty, value); }
12 }
13
14 // Using a DependencyProperty as the backing store for prop. This enables animation, styling, binding, etc...
15 public static readonly DependencyProperty propProperty =
16 DependencyProperty.Register("prop", typeof(int), typeof(UserControl), new PropertyMetadata(0));
17
18}
C#
1public class MainWindowViewModel : INotifyPropertyChanged
2{
3 private int _num;
4 public int num
5 {
6 get
7 {
8 return _num;
9 }
10 set
11 {
12 _num = value;
13 PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("num"));
14 }
15 }
16 public event PropertyChangedEventHandler PropertyChanged;
17
18 public MainWindowViewModel()
19 {
20 num = 10;
21 }
22}
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/08/20 12:55
2016/08/20 13:16
2016/08/20 13:47
2016/08/20 13:59
2016/08/20 15:04
2016/08/20 16:00