2012-09-13 12 views
0

不能做綁定之間的結合我的用戶和視圖模型

隨機(MyViewModel) - > VMTestValue(MyViewModel) - > TestUserControl(主窗口)

「MyViewModel」 是視圖模型的 「主窗口」

所有項目:http://rusfolder.com/32608140

TestUserControl.xaml

<UserControl x:Class="TestBinding.TestUserControl" 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      mc:Ignorable="d" 
      Background="Green" 
      d:DesignHeight="300" d:DesignWidth="300"> 
    <Grid> 
     <TextBlock Background="Gray" Margin="50" FontSize="18" Text="{Binding TestValue}" /> 
     <Rectangle Height="200" Width="{Binding TestValue}" Fill="#FFFF1717" /> 
    </Grid> 
</UserControl> 

TestUserControl.cs

... 
public TestUserControl() 
     { 
      InitializeComponent(); 

      this.DataContext = this; 
     } 

     public static readonly DependencyProperty TestValueProperty = 
      DependencyProperty.Register("TestValue", typeof(int), typeof(TestUserControl), 
       new FrameworkPropertyMetadata((int)255) 
       ); 

     public int TestValue 
     { 
      set { SetValue(TestValueProperty, value); } 
      get 
      { 
       return (int)GetValue(TestValueProperty); 
      } 
     } 
... 

MyViewModel.cs

using System; 

using System.Timers; 
using Microsoft.Practices.Prism.ViewModel; 

namespace TestBinding 
{ 
    class MyViewModel : NotificationObject 
    { 
     private int _VMTestValue; 

     private Timer timer; 
     private Random _random; 

     public MyViewModel() 
     { 
      _random=new Random(); 

      timer=new Timer(); 
      timer.Interval = 500; 
      timer.Elapsed += new ElapsedEventHandler(timer_Elapsed); 
      timer.Start(); 
     } 

     void timer_Elapsed(object sender, ElapsedEventArgs e) 
     { 
      VMTestValue = _random.Next(10, 300); 
     } 

     public int VMTestValue 
     { 
      set 
      { 
       _VMTestValue = value; 
       this.RaisePropertyChanged(() => this.VMTestValue); 
      } 
      get { return _VMTestValue; } 
     } 
    } 
} 

MainWindow.cs

... 
    public partial class MainWindow : Window 
    { 
     public MainWindow() 
     { 
      InitializeComponent(); 
      this.DataContext = new MyViewModel(); 
     } 
    } 
... 

MainWindow.xaml

... 
<TestBinding1:TestUserControl TestValue="{Binding VMTestValue}" /> 
<TextBlock VerticalAlignment="Bottom" Background="Blue" FontSize="20" Text="{Binding VMTestValue}" /> 
... 

爲什麼Ç一個不是我去用戶控制?

回答

5

除了狒狒我在我的項目中做到這一點:

刪除this.DataContext = this;及以下添加到您的XAML

<UserControl x:Class="TestBinding.TestUserControl" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     mc:Ignorable="d" 
     Background="Green" 
     d:DesignHeight="300" d:DesignWidth="300" 
     x:Name="uc"> 
<Grid> 
    <TextBlock Background="Gray" Margin="50" FontSize="18" Text="{Binding ElementName=uc,Path=TestValue}" /> 
    <Rectangle Height="200" Width="{Binding ElementName=uc,Path=TestValue}" Fill="#FFFF1717" /> 
</Grid> 
</UserControl> 
+0

這裏一些好點到用戶控件綁定http://stackoverflow.com/questions/11226843/data-binding-in-wpf-user-controls看到H.B.評論 – blindmeis

+0

爲什麼在DataContext中不起作用? – Ivan

+0

如果在usercontrol ctor中設置了datacontext,則永遠無法從「外部wpf」:)繼承datacontext – blindmeis

0

你應該讓你的用戶控件繼承的DataContext:

public TestUserControl() 
{ 
    InitializeComponent(); 

    //this.DataContext = this; 
} 

如果你想TestUserControl的DataContext的不斷本身,你可以使用的RelativeSource:

<TestBinding1:TestUserControl TestValue="{Binding DataContext.VMTestValue, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Window}}" /> 

或者使用的ElementName,讓我們說你的MainWindow命名爲mainWin

<TestBinding1:TestUserControl TestValue="{Binding VMTestValue, ElementName=mainWin}" />