C# 使用代码隐藏将数据绑定到列表选择器
我有一个用户控件,因为我有一个列表选择器。 代码是:C# 使用代码隐藏将数据绑定到列表选择器,c#,xaml,windows-phone-8,C#,Xaml,Windows Phone 8,我有一个用户控件,因为我有一个列表选择器。 代码是: <Grid x:Name="LayoutRoot" Background="{StaticResource PhoneChromeBrush}"> <Grid.RowDefinitions> <RowDefinition Height="auto"/> <RowDefinition Height="auto"/>
<Grid x:Name="LayoutRoot" Background="{StaticResource PhoneChromeBrush}">
<Grid.RowDefinitions>
<RowDefinition Height="auto"/>
<RowDefinition Height="auto"/>
<RowDefinition Height="auto"/>
</Grid.RowDefinitions>
<toolkit:ListPicker x:Name="lstPicker" Grid.Row="1" Margin="25,0,25,15" />
<StackPanel Grid.Row="2" Orientation="Horizontal" HorizontalAlignment="Center" >
<Button Width="200" x:Name="btnReAssign" Content="reassign" Margin="5"/>
<Button Width="200" x:Name="btnCancel" Content="cancel" Margin="5"/>
</StackPanel>
</Grid>
这包含列表选择器。
现在我将这个listpicker与我的数据对象绑定
myTaskPopUpWindow.lstPicker.ItemsSource = GetRegisterUserOC;
但是类名显示在列表选择器中,而不是属性中。我不知道如何将属性之一绑定到此列表选择器。我可以从代码隐藏中绑定一个属性吗,因为我不想在用户控件中进行更改。通常,您可以执行以下操作:
<toolkit:ListPicker x:Name="lstPicker" Grid.Row="1" Margin="25,0,25,15" >
<toolkit:ListPicker.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding YourDisplayPropertyOnObject}"/>
</DataTemplate>
</toolkit:ListPicker.ItemTemplate>
</toolkit:ListPicker>
如果您希望采用更懒惰的方法,您可以简单地覆盖要绑定的对象上的
ToString()
属性,以显示您想要的方式。我从未使用过ListPicker
,但您不能将displaymberpath
设置为要显示的属性(例如名称)
列表选择器位于用户控件中,我在页面中使用此用户控件,我希望将数据绑定到此列表选择器。那么有可能从代码背后绑定它吗?正如我所说的,您可以覆盖绑定到的对象上的
ToString()
属性,因为如果您没有DataTemplate
,它基本上会调用该属性。
<toolkit:ListPicker x:Name="lstPicker" Grid.Row="1" Margin="25,0,25,15" >
<toolkit:ListPicker.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding YourDisplayPropertyOnObject}"/>
</DataTemplate>
</toolkit:ListPicker.ItemTemplate>
</toolkit:ListPicker>
<Grid x:Name="LayoutRoot" Background="{StaticResource PhoneChromeBrush}">
<Grid.RowDefinitions>
<RowDefinition Height="auto"/>
<RowDefinition Height="auto"/>
<RowDefinition Height="auto"/>
</Grid.RowDefinitions>
<TextBlock x:Name="tbkTitle" Margin="25,25,25,15" FontSize="32" />
<toolkit:ListPicker x:Name="lstPicker" Grid.Row="1" Margin="25,0,25,15" DisplayMemberPath="Property" />
<StackPanel Grid.Row="2" Orientation="Horizontal" HorizontalAlignment="Center" >
<Button Width="200" x:Name="btnReAssign" Content="reassign" Margin="5"/>
<Button Width="200" x:Name="btnCancel" Content="cancel" Margin="5"/>
</StackPanel>
</Grid>
myTaskPopUpWindow.lstPicker.DisplayMemberPath = PropertyName;