小编Iva*_*van的帖子

在我的usercontrol和ViewModel之间绑定

不能做绑定

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

"MyViewModel"是"MainWindow"的ViewModel

所有项目: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>
Run Code Online (Sandbox Code Playgroud)

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);
            }
        }
...
Run Code Online (Sandbox Code Playgroud)

MyViewModel.cs

using …
Run Code Online (Sandbox Code Playgroud)

binding dependencies prism properties mvvm

0
推荐指数
1
解决办法
1万
查看次数

标签 统计

binding ×1

dependencies ×1

mvvm ×1

prism ×1

properties ×1