本文介绍了我的用户和视图模型之间的绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
不能做绑定
随机(MyViewModel) - > VMTestValue(MyViewModel) - > TestUserControl(主窗口)
Random (MyViewModel) -> VMTestValue (MyViewModel) -> TestUserControl (MainWindow)
MyViewModel是视图模型为主窗口
"MyViewModel" is ViewModel for "MainWindow"
所有的项目:
TestUserControl.xaml
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
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
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
MainWindow.cs
...
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.DataContext = new MyViewModel();
}
}
...
MainWindow.xaml
MainWindow.xaml
...
<TestBinding1:TestUserControl TestValue="{Binding VMTestValue}" />
<TextBlock VerticalAlignment="Bottom" Background="Blue" FontSize="20" Text="{Binding VMTestValue}" />
...
为什么我不能去到该用户控件?
推荐答案
除了狒狒我在我的项目做到这一点:
in addition to baboon i do this in my projects:
删除 this.DataContext =这一点; 并添加以下到您的XAML
remove this.DataContext = this; and add the following to your 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>
这篇关于我的用户和视图模型之间的绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!