Silverlight绑定-CommandParameter到父级(帮助删除x:Name)
你知道我如何绑定到列表框而不使用x:名称来表示下面的代码吗?我在ElementName=myList中使用xnameSilverlight绑定-CommandParameter到父级(帮助删除x:Name),silverlight,xaml,data-binding,windows-phone-7,command,Silverlight,Xaml,Data Binding,Windows Phone 7,Command,你知道我如何绑定到列表框而不使用x:名称来表示下面的代码吗?我在ElementName=myList中使用xname <ListBox x:Name="myList" Grid.Row="1" Height="auto" ItemsSource="{Binding Path=ListItems}" ItemContainerStyle="{StaticResource StretchedItemContainerStyle}" ScrollViewer.VerticalScrollBarV
<ListBox x:Name="myList" Grid.Row="1" Height="auto" ItemsSource="{Binding Path=ListItems}" ItemContainerStyle="{StaticResource StretchedItemContainerStyle}" ScrollViewer.VerticalScrollBarVisibility="Disabled" Background="Transparent">
<wi:Interaction.Triggers>
<wi:EventTrigger EventName="SelectionChanged">
<wi:InvokeCommandAction Command="{Binding Source={StaticResource Locator}, Path=ViewModel.Command}" CommandParameter="{Binding SelectedItem, ElementName=myList}" />
</wi:EventTrigger>
</wi:Interaction.Triggers>
<ListBox.ItemTemplate>
我真的不想每次使用InvokeCommandAction时都为我的控件定义一个名称。注意:wi是针对SL4/WP7.1的Windows交互
谢谢 我不确定这是否真的可能,所以当我怀疑并等待其他人回答时,我会推荐一种不同的方法 您可以使用附加的行为来实现这一点,这是一种简单而整洁的方法,提供了更多干净的代码 我已经发布了一个框架
列表框所需的是选择扩展
。如果您只需要代码,请查看以下内容:。(来源是麻省理工学院许可的)
这意味着您只需将代码更改为以下内容:
<ListBox ext:SelectorExtension.Command="{Binding Source={StaticResource Locator}, Path=ViewModel.Command}"
Grid.Row="1"
Height="Auto"
ItemsSource="{Binding Path=ListItems}"
ItemContainerStyle="{StaticResource StretchedItemContainerStyle}"
ScrollViewer.VerticalScrollBarVisibility="Disabled"
Background="Transparent">
...
...
如果ext
是xmlns:ext=“clr namespace:ToolkitExtensions;clr assembly=ToolkitExtensions”
我不确定这是否真的可行,所以在我怀疑并等待其他人回答的同时,我会推荐一种不同的方法
您可以使用附加的行为来实现这一点,这是一种简单而整洁的方法,提供了更多干净的代码
我已经发布了一个框架
列表框所需的是选择扩展
。如果您只需要代码,请查看以下内容:。(来源是麻省理工学院许可的)
这意味着您只需将代码更改为以下内容:
<ListBox ext:SelectorExtension.Command="{Binding Source={StaticResource Locator}, Path=ViewModel.Command}"
Grid.Row="1"
Height="Auto"
ItemsSource="{Binding Path=ListItems}"
ItemContainerStyle="{StaticResource StretchedItemContainerStyle}"
ScrollViewer.VerticalScrollBarVisibility="Disabled"
Background="Transparent">
...
...
其中ext
为xmlns:ext=“clr命名空间:ToolkitExtensions;clr assembly=ToolkitExtensions”
尝试绑定到“SelectedItem”,并完全消除传递CommandParameter的需要。不要忘记模式=双向。在ViewModel中的TifyPropertyChanged getter/setter中添加“SelectedListItem”并绑定到该属性
<ListBox SelectedItem={Binding SelectedListItem, Mode=TwoWay} Grid.Row="1" Height="auto" ItemsSource="{Binding Path=ListItems}" ItemContainerStyle="{StaticResource StretchedItemContainerStyle}" ScrollViewer.VerticalScrollBarVisibility="Disabled" Background="Transparent">
<wi:Interaction.Triggers>
<wi:EventTrigger EventName="SelectionChanged">
<wi:InvokeCommandAction Command="{Binding Source={StaticResource Locator}, Path=ViewModel.Command}" />
</wi:EventTrigger>
</wi:Interaction.Triggers>
<ListBox.ItemTemplate>
</ListBox>
尝试绑定到“SelectedItem”,并完全消除传递CommandParameter的需要。不要忘记模式=双向。在ViewModel中的TifyPropertyChanged getter/setter中添加“SelectedListItem”并绑定到该属性
<ListBox SelectedItem={Binding SelectedListItem, Mode=TwoWay} Grid.Row="1" Height="auto" ItemsSource="{Binding Path=ListItems}" ItemContainerStyle="{StaticResource StretchedItemContainerStyle}" ScrollViewer.VerticalScrollBarVisibility="Disabled" Background="Transparent">
<wi:Interaction.Triggers>
<wi:EventTrigger EventName="SelectionChanged">
<wi:InvokeCommandAction Command="{Binding Source={StaticResource Locator}, Path=ViewModel.Command}" />
</wi:EventTrigger>
</wi:Interaction.Triggers>
<ListBox.ItemTemplate>
</ListBox>
这将起作用,但它的代码比我希望的要多一些。希望有一种方法可以说CommandParameter=“{Binding SelectedItem,ElementName={RelativeSource=Parent}”@John-RelativeSource.FindAncestor
在Silverlight 4中不受支持。是的,这是一个令人沮丧的问题,但这是一个紧凑框架的方法。Vidalsason的回答很贴切,将使您的代码在将来更易于维护。这会起作用,但它的代码比我想要的要多一些。我希望有一种方式来表示commandParameterter=“{Binding SelectedItem,ElementName={RelativeSource=Parent}”。@John-RelativeSource.FindAncestor
在Silverlight 4中不受支持。是的,这是一个令人沮丧的问题,但这是一个紧凑框架的方法。Vidalsason的回答很贴切,将使您的代码在未来更易于维护。