DataTemplate中的Silverlight组合框

时间:2015-01-29 14:23:24

标签: c# silverlight data-binding combobox telerik

我已经在xaml中为c#类定义了一个数据模板,如下所示

<DataTemplate x:Key="ApplicationTemplate">
                <StackPanel Orientation="Vertical">
                    <telerik:RadComboBox DisplayMemberPath="Name"
                                         ItemsSource="{Binding CurrentItem.Apps, RelativeSource={RelativeSource FindAncestor, AncestorType=telerik:RadDataForm}}"
                                         IsEnabled="{Binding IsReadOnly, RelativeSource={RelativeSource AncestorType=ContentControl}, Converter={StaticResource BooleanInverterConverter}}" />                   
                </StackPanel>
            </DataTemplate>

我的“Apps”Enumerable由这个对象组成:

public class InteractiveApplicationModel : ValidatingModel
    {
        public string Name
        {
            get { return GetProperty(() => Name); }
            set { SetProperty(() => Name, value); }
        }

        public string Type
        {
            get { return GetProperty(() => Type); }
            set { SetProperty(() => Type, value); }
        }

        public string URL
        {
            get { return GetProperty(() => URL); }
            set { SetProperty(() => URL, value); }
        }

        public string Image
        {
            get { return GetProperty(() => Image); }
            set { SetProperty(() => Image, value); }
        }

        public InteractiveApplicationModel()
        {
            this.Type = string.Empty;
            this.Name = string.Empty;
            this.URL = string.Empty;
            this.Image = string.Empty;
        }

        public InteractiveApplicationModel(string name, string type, string url, string image)
        {
            this.Name = name;
            this.Type = type;
            this.URL = url;
            this.Image = image;
        }
    }
}

在View模型中,我有:

public IList<InteractiveApplicationModel> Apps
        {
            get
            {
                return new List<InteractiveApplicationModel>()
                    {
                        new InteractiveApplicationModel(null,null,null,null),
                        new InteractiveApplicationModel("name","type","url","image"),
                        new InteractiveApplicationModel("name2","type2","url2","image2")
                    };
            }
        }

然后我有一个表单页面,它加载包含“InteractiveApplicationModel”对象的“复杂”对象,并使用数据模板显示这些对象。

除了一件事,一切都在起作用。我需要组合框的选定值与我的“复杂”对象中“InteractiveApplicationModel”对象的值相同。

我的“复杂”对象最多可包含5个“InteractiveApplicationModel”对象。

1 个答案:

答案 0 :(得分:0)

您必须绑定RadComboBox上的SelectedItem属性

SelectedItem="{Binding SelectedInteractiveApplicationModel, [.... ancestor ....] Mode=TwoWay}"

在ViewModel上使用Selected属性

public InteractiveApplicationModel SelectedInteractiveApplicationModel
    {
        get { return GetProperty(() => SelectedInteractiveApplicationModel); }
        set { SetProperty(() => SelectedInteractiveApplicationModel, value); }
    }