【发布时间】:2011-02-03 12:15:25
【问题描述】:
我在 Silverlight 3 中有以下 ItemsControl。
<ItemsControl ItemsSource="{Binding ValueCollectionList}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Button x:Name="MyBtn" Height="40" Content="{Binding Name}"
Tag="{Binding Value}"
cmd:ButtonBaseExtensions.Command="{Binding ElementName=LayoutRoot, Path=ButtonCommand}"
cmd:ButtonBaseExtensions.CommandParameter="{Binding ElementName=MyBtn, Path=Tag}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
问题是我将 ItemsControl 绑定到 ViewModel 中的集合,但我需要按钮来触发 ViewModel 上的命令,该命令在按钮的 DataContext 中当然不可用,因为它只包含集合。
我可以通过将 ViewModel 设置为 Resource 然后将其作为 StaticResource 绑定到它来触发命令,但我想知道为什么元素到元素的绑定在这种情况下不起作用。我不希望使用 StaticResource 绑定,因为这需要 ViewModel 上的默认构造函数,因此我无法轻松注入数据。
更新
我正在慢慢解决这个问题...看着 Peter 的建议,我意识到由于我的页面设置,我可能会遇到更严重的绑定问题。我有两个可能的障碍,但首先要做的是。
上面的我的 Items 控件被包装在另一个绑定到可观察集合的 items 控件中。我移动了我的项目控件,使其成为根项目控件的直接子项。它被包裹在另一个控件中,我将得到它。因此,我尝试将元素绑定到项目控件 ControlItemList,但它是一个集合,因此它无法在该集合中找到我的 ButtonCommand 方法。我需要做的是绑定到该集合中的一个项目。如何绑定到集合中的单个项目?
<Grid x:Name="LayoutRoot" Background="White"><!-- DataContext="{Binding Path=., Source={StaticResource lvvm}}">-->
<StackPanel Orientation="Vertical">
<ItemsControl x:Name="ControlItemList" ItemsSource="{Binding}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition MinWidth="100" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<TextBlock x:Name="ControlName" Grid.Column="0" Text="{Binding Name}" VerticalAlignment="Center" />
<ItemsControl ItemsSource="{Binding ValueCollectionList}">
<ItemsControl.ItemTemplate>
<DataTemplate>
所以,假设我可以让上述工作正常工作,另一个障碍是我的项目控件被包装在另一个用户控件中,我用来获取 DataTemplateSelector 类型的功能。我认为这个控件可能会阻止我访问父 DataContext。你能走多远的树有限制吗?
<common:DeviceControlTemplateSelector Grid.Column="1" FieldType="{Binding ValueType}" Margin="0,2,0,2">
<common:DeviceControlTemplateSelector.StringTemplate>
<DataTemplate>
<TextBox Text="{Binding Value, Mode=TwoWay}" Width="100"/>
</DataTemplate>
</common:DeviceControlTemplateSelector.StringTemplate>
<common:DeviceControlTemplateSelector.DateTimeTemplate>
<DataTemplate>
<TextBox Text="this is date time binding" Width="100"/>
</DataTemplate>
</common:DeviceControlTemplateSelector.DateTimeTemplate>
<common:DeviceControlTemplateSelector.BooleanTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding Value, Mode=TwoWay}" />
</DataTemplate>
</common:DeviceControlTemplateSelector.BooleanTemplate>
<common:DeviceControlTemplateSelector.IntegerTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding ValueCollection}" DisplayMemberPath="Value" SelectedIndex="{Binding Value, Mode=TwoWay}" >
</ComboBox>
</DataTemplate>
</common:DeviceControlTemplateSelector.IntegerTemplate>
<common:DeviceControlTemplateSelector.MultiButtonTemplate>
<DataTemplate>
<ItemsControl ItemsSource="{Binding ValueCollectionList}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Button x:Name="MyBtn"
Height="40" Content="{Binding Name}"
Tag="{Binding Value}"
cmd:ButtonBaseExtensions.Command="{Binding ElementName=ControlItemList, Path=DataContext.ButtonCommand}"
cmd:ButtonBaseExtensions.CommandParameter="{Binding Value}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</DataTemplate>
</common:DeviceControlTemplateSelector.MultiButtonTemplate>
<Button x:Name="MyBtn"
Height="40" Content="{Binding Name}"
Tag="{Binding Value}"
cmd:ButtonBaseExtensions.Command="{Binding ElementName=ControlItemList, Path=ButtonCommand}"
cmd:ButtonBaseExtensions.CommandParameter="{Binding Value}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
再次感谢大家的帮助!
【问题讨论】:
标签: silverlight data-binding silverlight-3.0 mvvm