【发布时间】:2011-03-27 15:19:21
【问题描述】:
我正在开发自定义控件。
以下代码是用 generic.xaml 编写的
<Style TargetType="local:TwoListBox">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="local:TwoListBox">
<StackPanel Orientation="Horizontal">
<ListBox x:name="ListBoxForBasic" ItemsSource="{Binding}" DisplayMemberPath="NumValue" Margin="10"/>
<ListBox x:name="ListBoxForSorting" ItemsSource="{Binding}" DisplayMemberPath="NumValue" Margin="10"/>
</StackPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
很简单,我有两个ListBox,我想把下面的数据绑定到两个ListBox
public class SampleData
{
public int Num { get; set; }
public int NumValue { get; set; }
}
public class SampleDataList : List<SampleData>
{
public SampleDataList()
{
Add(new SampleData{ Num=1, NumValue=10});
Add(new SampleData { Num = 2, NumValue = 50 });
Add(new SampleData { Num = 3, NumValue = 20 });
Add(new SampleData { Num = 4, NumValue = 40 });
Add(new SampleData { Num = 5, NumValue = 30 });
}
}
在 MainPage.xaml 中,我使用了 TwoListBox 自定义控件的示例数据,如下所示:
<local:TwoListBox DataContext="{StaticResource sampleData}"/>
如果按 F5,TwoListBox 自定义控件如下所示:
10 10
50 50
20 20
40 40
30 30
但是,我想像这样为第二个 ListBox(ListBoxForSorting) 绑定排序数据:
10 10
50 20
20 30
40 40
30 50
在这种情况下,我该怎么办?
提前致谢
【问题讨论】:
标签: silverlight data-binding listbox custom-controls