wpf控制绑定错误

时间:2014-03-12 15:12:51

标签: wpf binding controls

我有2个项目的解决方案: 1.包含控件 2.主要观点

从主视图

我用绑定值调用控件,例如:

我的控制xaml:

<Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="Auto" />
        </Grid.ColumnDefinitions>

        <TextBox
            Grid.Row="0"
            Grid.Column="0"
            Margin="5"
            Text="{Binding NewItem, UpdateSourceTrigger=PropertyChanged}"
            />

        <ListBox
            Grid.Row="1"
            Grid.Column="0"
            Margin="5"
            ItemsSource="{Binding ExistingItems}"
            />

及其背后的代码:

/// <summary>
/// Interaction logic for ItemModifierControl.xaml
/// </summary>
public partial class ItemModifierControl : UserControl
{

    public static readonly DependencyProperty ExistingItemsProperty =
        DependencyProperty.Register("ExistingItems", typeof (ObservableCollection<string>), typeof (ItemModifierControl), 
        new PropertyMetadata(default(ObservableCollection<string>)));


    public ObservableCollection<string> ExistingItems
    {
        get { return (ObservableCollection<string>) GetValue(ExistingItemsProperty); }
        set { SetValue(ExistingItemsProperty, value); }
    }


    public ItemModifierControl()
    {
        InitializeComponent();

        this.Loaded += OnLoaded;
        ExistingItems = new ObservableCollection<string>();
    }

在主视图中我正在调用这样的控件:

<Window x:Class="ItemModifier.Test.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:m="clr-namespace:ItemModifier;assembly=ItemModifier"
        DataContext="{Binding Source={StaticResource Locator}, Path=Main}"
        Title="{Binding DisplayName}" Height="350" Width="525">
    <Grid>
        <Viewbox
            >
            <Border
                BorderBrush="Black"
                BorderThickness="1"
                >
            <m:ItemModifierControl 
                Height="150"
                Width="150"
                ExistingItems="{Binding MyItems}"
                AllowDuplicates="{Binding IsAllowDuplicates}"
                />
                </Border>
            </Viewbox>
    </Grid>
</Window>

查看模型(MainViewModel.cs):

public class MainViewModel : ViewModelBase
{
    public ObservableCollection<string> MyItems { get; set; }

    public bool IsAllowDuplicates { get; set; }

    public string DisplayName { get; set; }

    public MainViewModel()
    {
        IsAllowDuplicates = true;

        PopulateMyItems();
    }

    private void PopulateMyItems()
    {
        DisplayName = "Control Application";

        MyItems= new ObservableCollection<string>
            {
                "MyItem1", "MyItem2", "MyItem3", "MyItem4"
            };
    }
}

对viewmodel的绑定工作正常,因为应用程序的标题被绑定到DisplayName。

而MyItems是我的视图模型中带有数据的ObservableCollection。 我得到的错误是:

System.Windows.Data Error: 40 : BindingExpression path error: 'MyItems' property not found on 'object' ''ItemModifierControl' (Name='')'. BindingExpression:Path=MyItems; DataItem='ItemModifierControl' (Name=''); target element is 'ItemModifierControl' (Name=''); target property is 'ExistingItems' (type 'ObservableCollection`1')

0 个答案:

没有答案