我试图在这里做的是将按钮包含的对象绑定到我的ViewModel中的属性以便使用它,但无法解决我的问题 . 我读到了Tag属性,但无法完全理解它如何绑定以及如何使用它 .
我的按钮:
<DataTemplate x:Key="Template">
<GroupBox>
<WrapPanel>
<Button x:Name="Button"
FontSize="10"
Height="80" Width="80"
Content="{Binding Name}"
</Button>
</WrapPanel>
</GroupBox>
</DataTemplate>
我使用ObservableCollection中的数据填充我的按钮
<WrapPanel x:Name="OrderButtons"
VerticalAlignment="Bottom">
<ScrollViewer x:Name="OrderButtons1"
Margin="3">
<ItemsControl
ItemTemplate="{StaticResource OrderTemplate}"
ItemsSource="{Binding Source={StaticResource ObservableCollectionWithData}}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel Orientation="Horizontal">
</WrapPanel>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
</ScrollViewer>
</WrapPanel>
我的ViewModel
public class OrderViewModel : ObservableCollection<Order>
{
public Item OrderItem{ get; set; }
public OrderViewModel()
{
}
}
我应该怎么做才能将我的Button(包含Item类型的对象(我的自定义类))绑定到我的ViewModel的OrderItem属性?
我尝试的是:
<Button x:Name="OrderButton"
FontSize="10"
Height="80" Width="80"
Content="{Binding Name}"
Tag="{Binding OrderItem, Source={StaticResource OrderViewModel}}"
</Button>
1 回答
在我看来,你正在创造一个'选择器';即,单击该按钮时,您希望视图模型的OrderItem属性包含单击的“项” .
如果是这种情况,最好使用ListBox,如下所示:
您还应该在ViewModel中实现
INotifyPropertyChanged
.希望能帮助到你 .