0

不能做绑定

随机 (MyViewModel) -> VMTestValue (MyViewModel) -> TestUserControl (MainWindow)

“MyViewModel”是“MainWindow”的 ViewModel

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

测试用户控件.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>

测试用户控件.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; }
        }
    }
}

主窗口.cs

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

主窗口.xaml

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

为什么我不能去UserControl?

4

2 回答 2

6

除了狒狒,我在我的项目中这样做:

删除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>
于 2012-09-13T12:06:40.060 回答
1

您应该让您的 UserControl 继承 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}" />
于 2012-09-13T10:45:09.627 回答