【问题标题】:Correct use of databinding with a listbox in with Binding Object正确使用数据绑定与绑定对象中的列表框
【发布时间】:2012-08-22 04:01:09
【问题描述】:

您好,提前感谢您的帮助。我在将数据绑定与具有我希望在列表框中显示的属性的对象列表一起使用时遇到问题。

我的 Silverlight 子窗口中有一个列表框,其 xaml 如下所示:

<StackPanel Orientation="Vertical" Width="235">
                        <sdk:Label Content="Servers" HorizontalAlignment="Center"></sdk:Label>
                        <!--new group servers list box-->
                        <ListBox x:Name="NewGroupServersLB" Height="150" Width="200" ItemsSource="{Binding}" SelectionChanged="NewGroupServersLB_SelectionChanged" >
                            <ListBox.ItemTemplate>
                                <DataTemplate>
                                    <TextBlock Text="{Binding Path=ServerName}"></TextBlock>
                                </DataTemplate>
                            </ListBox.ItemTemplate>
                        </ListBox>
                    </StackPanel>

我在后面的代码中使用 Binding 对象实例设置它的 DataContext:

/*create new binding source object */
 Binding serversBinding = new Binding();
 /*set data source */
 serversBinding.Source = childServersList;
 /*set the data source for the new group servers listbox*/
 NewGroupServersLB.DataContext = serversBinding;

我可以通过调试看到 Binding 对象的源有两个成员,并且它们是来自我的 childServersList 的适当服务器对象实例。但是,当我运行它时,列表框中没有任何内容。但是,当我在后面的代码中将列表框的数据上下文直接设置为服务器列表时,服务器名称会显示在列表框中。你能告诉我这是为什么还是我做错了什么?我尝试使用 Binding 对象的原因是因为我想用第一个列表框中所选服务器对象的子成员填充另一个列表框。提前感谢您的帮助,对于这个问题的冗长,我深表歉意。

如果有帮助,我存储在列表中的服务器对象如下所示:

[XmlRoot("Server")]
public class RegisterServerObject
{
    public RegisterServerObject() { }

    [XmlElement("ServerID")]
    [Browsable(false)]//not displayed in grids
    [EditorBrowsable(EditorBrowsableState.Never)]//not displayed by intellisense
    public string ServerIDString
    {
        set
        {
            ServerID = Convert.ToInt32(value);//convert the value passed in by xml to your type
        }
        get
        {
            return Convert.ToString(ServerID);
        }
    }

    [XmlIgnore]
    public int ServerID { get; set; }

    [XmlElement("GroupID")]
    public string GroupIDString
    {
        get
        {
            return this.GroupID.ToString();
        }
        set
        {
            if (string.IsNullOrEmpty(value))
            {
                this.GroupID = 0;
            }
            else
            {
                this.GroupID = int.Parse(value);
            }
        }
    }

    [XmlIgnore]
    public int GroupID { get; set; }

    [XmlElement("ParentID")]
    public int ParentID { get; set; }

    [XmlElement("ServerName")]
    public string ServerName { get; set; }

    [XmlElement("User")]
    public string User { get; set; }

    [XmlElement("UID")]
    public int Uid { get; set; }

    [XmlElement("PWD")]
    public string Domain { get; set; }

    [XmlElement("Location")]
    public string Location { get; set; }

    [XmlArray(ElementName = "AssociatedModules")]
    [XmlArrayItem(ElementName = "Module")]
    public List<RegisterModuleObject> AssociatedModules { get; set; }

【问题讨论】:

  • 尝试在代码隐藏中设置 ItemsSource 属性,而不是设置 DataContext。从您的代码中删除“ItemSource={Binding}”。

标签: c# .net silverlight data-binding silverlight-5.0


【解决方案1】:

有几种不同的方法可以指定绑定源(数据绑定到的对象)。首先是指定DataContext。这可以在 XAML 中或通过代码完成。

NewGroupServersLB.DataContext = childServersList;

请注意,DataContext 设置为源对象。

如果您使用的是 Binding 对象,则应将 Binding 对象的 Source 设置为您的绑定源。

MyData myDataObject = new MyData(DateTime.Now);      
Binding myBinding = new Binding("MyDataProperty");
myBinding.Source = myDataObject;
myText.SetBinding(TextBlock.TextProperty, myBinding);

在这种情况下,您正在设置 Binding 对象,然后将其应用于控件。 DataContext 未设置,但如果已设置,将被忽略,因为 Binding 对象将优先。

对于您的特定示例,您可能需要阅读数据绑定文档的此特定部分:

http://msdn.microsoft.com/en-us/library/ms752347.aspx#master_detail_scenario

【讨论】:

  • 好的,感谢您提供此信息。我通常通过代码指定 DataContext 而不是使用 Binding 对象。我不确定在这种情况下 Binding 是否适合我..所以如果该对象是属性,您可以使用对象设置构造 Binding 属性另一个对象?
猜你喜欢
  • 2010-10-10
  • 1970-01-01
  • 1970-01-01
  • 2014-01-15
  • 2011-01-28
  • 1970-01-01
  • 2017-05-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多