DisplayMemberPath不起作用,TreeView,Silverlight

尼古拉·卢科维奇

好的,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;
    }
}
尼古拉·卢科维奇

我修复了它,我需要添加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>

现在,它就像一种魅力!

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章