小编Aab*_*Ali的帖子

UserControl中的依赖属性绑定

我的解决方案是在MVVM中实现的.视图是一个托管用户控件的窗口.我已为此userControl创建了一个依赖项属性,如下所示:

public static DependencyProperty ListProperty = DependencyProperty.Register(
      "ItemsList", typeof(List<RequiredClass>), typeof(UsercontrolTest));

public List<RequiredClass> ItemsList
{
    get { return (List<RequiredClass>)GetValue(ListProperty); }
    set
    {
        SetValue(ListProperty, value);
    }
}
Run Code Online (Sandbox Code Playgroud)

此属性绑定到xaml中的viewmodel属性(ListOfItems):

  <Window x:Class="TestProject.MainWindow"
          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
          xmlns:Test="clr-namespace:TestProject"
          Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition></RowDefinition>             
            <RowDefinition></RowDefinition>
        </Grid.RowDefinitions>
        <Test:UserControlTest Grid.Row="0" ItemsList="{Binding Path=ListOfItems}" /> 
        <Button Grid.Row="1" Content="AddItems" Click="Button_Click" />
    </Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)

此外,我已将codebehind窗口的datacontext初始化为viewmodel.问题是绑定似乎永远不会发生,并且永远不会为依赖项属性调用set属性.我在这里错过了什么吗?

c# data-binding wpf xaml mvvm

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

标签 统计

c# ×1

data-binding ×1

mvvm ×1

wpf ×1

xaml ×1