DisplayMemberPath无法正常工作,TreeView,Silverlight

时间:2014-06-19 13:57:29

标签: c# silverlight xaml treeview

好吧,所以DisplayMemberPath根本不工作,我整天都试着修复它,我在互联网上找不到任何东西......我试着用代码完成所有的东西但它仍然没有不行...... 这是xaml:

<Grid x:Name="gdMain" Background="Transparent" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
    <Grid.Resources>
        <local:CountryList x:Key="smth"/>
    </Grid.Resources>
    <sdk:TreeView ItemsSource="{Binding Source={StaticResource smth}, Path=getCountries}" DisplayMemberPath="Name" x:FieldModifier="public" x:Name="tvMain" Background="Transparent" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" BorderBrush="Transparent">
    </sdk:TreeView>
</Grid>

这是背后的代码:

public class CountryList 
{
    private ObservableCollection<Country> ocl = new ObservableCollection<Country>();
    public CountryList()
    {
        ocl.Add(new Country()
        {
            Name = "Bolivia, Plurinational State of",
            Alpha2 = "BO",
            Alpha3 = "BOL",
            Iso = "ISO 3166-2:BO",
            FonPrefix = "+591"
        });
        ocl.Add(new Country()
        {
            Name = "China",
            Alpha2 = "CN",
            Alpha3 = "CHN",
            Iso = "ISO 3166-2:CN",
            FonPrefix = "+86"
        });
        ocl.Add(new Country()
        {
            Name = "Andorra",
            Alpha2 = "AD",
            Alpha3 = "AND",
            Iso = "ISO 3166-2:AD",
            FonPrefix = "+376"
        });
        ocl.Add(new Country()
        {
            Name = "Moldova, Republic of",
            Alpha2 = "MD",
            Alpha3 = "MDA",
            Iso = "ISO 3166-2:MD",
            FonPrefix = "+373"
        });
        ocl.Add(new Country()
        {
            Name = "Poland",
            Alpha2 = "PL",
            Alpha3 = "POL",
            Iso = "ISO 3166-2:PL",
            FonPrefix = "+48"
        });
        ocl.Add(new Country()
        {
            Name = "Iceland",
            Alpha2 = "IS",
            Alpha3 = "ISL",
            Iso = "ISO 3166-2:IS",
            FonPrefix = "+354"
        });
        ocl.Add(new Country()
        {
            Name = "Colombia",
            Alpha2 = "CO",
            Alpha3 = "COL",
            Iso = "ISO 3166-2:CO",
            FonPrefix = "+57"
        });
        ocl.Add(new Country()
        {
            Name = "Bahrain",
            Alpha2 = "BH",
            Alpha3 = "BHR",
            Iso = "ISO 3166-2:BH",
            FonPrefix = "+973"
        });
        ocl.Add(new Country()
        {
            Name = "Dominican Republic",
            Alpha2 = "DO",
            Alpha3 = "DOM",
            Iso = "ISO 3166-2:DO",
            FonPrefix = "+1 809, +1 829, +1 849"
        });
        ocl.Add(new Country()
        {
            Name = "Nicaragua",
            Alpha2 = "NI",
            Alpha3 = "NIC",
            Iso = "ISO 3166-2:NI",
            FonPrefix = "+505"
        });
    }
    public  ObservableCollection<Country> getCountries
    {
        get
        {   
            return ocl;     
        }
    }
}

保存树视图的用户控件的构造函数。

public partial class ucTreeView : ucBaseClass
{

    public ucTreeView()
    {

            InitializeComponent();
            tvMain.ItemsSource = new CountryList().getCountries;
            tvMain.DisplayMemberPath = "Name";
            this.DataContext = this;
    }
}

2 个答案:

答案 0 :(得分:0)

好的,因为你没有在这里设置应用程序的DataContext是你需要做的(非常非常简化的版本):

在您的窗口下,您应该拥有以下内容:

public partial class Window1 : Window
{
    public ObservableCollection<Employee> empList { get; set; }
    public Window1()
    {
        InitializeComponent();
        empList = new ObservableCollection<Employee>();
        empList.Add(new Employee("John", "Michael"));
        empList.Add(new Employee("Freddy", "Rajec"));

        this.DataContext = this;//important line, wihtout this line your window will not get access to objects
    }
}
public class Employee
{
    public string Name { get; set; }
    public string Surname { get; set; }
    public Employee(string sName, string sSurname)
    {
        Name = sName;
        Surname = sSurname;
    }
}

然后在你的窗口中设置以下内容:

<ListBox ItemsSource="{Binding empList}" DisplayMemberPath="Name"/>

如果你使用MVVM模式,它会略有不同(因为数据Context成为视图模型)但概念或多或少相同。

修改

以下内容如何:

public partial class ucTreeView : ucBaseClass
{
    public ObservableCollection<CountryList> country_List{ get; set; }
    public ucTreeView()
    {
            InitializeComponent();
            country_List = new ObservableCollection<CountryList>();
            CountryList cList = new CountryList();
            country_List = cList.getCountries;//retrieve country list
            this.DataContext = this
    }
}

并在你的窗口下:

ItemsSource="{Binding country_List}, DisplayMemberPath="InputWhateverYouWantToShowHere"}

答案 1 :(得分:0)

我修复了它,我需要的是添加ItemTemplate和DataTemplate,如下所示:

<sdk:TreeView ItemsSource="{Binding Source={StaticResource smth}, Path=getCountries}"  HorizontalAlignment="Left" Height="223" Margin="135,58,0,0" VerticalAlignment="Top" Width="243">
        <sdk:TreeView.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding Name}"/>
            </DataTemplate>
        </sdk:TreeView.ItemTemplate>
    </sdk:TreeView>

现在它就像一个魅力!