使用visual studio 2015社区和.NET 4.5
我有一个特定于我的应用用户的单选按钮的动态列表。当用户使用Web服务进行身份验证时,将动态创建按钮列表。列表是列表存储在ObservableCollection中。我已经将它绑定到XAML并且它正确呈现,并且它在选择后正确地存储了isSelected。
我想要做的是当用户选择服务类型(点击单选按钮)时有一个事件触发器。我已经阅读了很多例子,但他们硬编码单选按钮,但在我的情况下,我需要按钮是动态的。
如何将XAML中的按钮单击绑定到ViewModel?
ServiceCost.cs
public class ServiceCost
{
public long pst_id { get; set; }
public string pst_description_short { get; set; }
public decimal pst_cost { get; set; }
private bool _isSelected;
public bool IsSelected
{
get { return _isSelected; }
set{if (value != _isSelected) { _isSelected = value; } }
}
public string DescriptionPrice
{
get
{
return string.Format(Strings.ServiceCost_DescriptionPrice, this.pst_description_short, pst_cost.ToString());
}
}
...
XAML:
...
<GroupBox
Grid.Row="1"
Grid.ColumnSpan="2"
Grid.Column="2"
FontWeight="Bold"
Header="Select Service Type"
Margin="0,7,0,0"
Padding="2">
<ItemsControl FontWeight="Normal" ItemsSource="{Binding Path=ServiceTypes}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<RadioButton
Content="{Binding Path=DescriptionPrice}"
IsChecked="{Binding Path=IsSelected}"
GroupName="ServiceType"
Margin="2,3.5"
/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</GroupBox>
...
视图模型:
...
private ObservableCollection<ServiceCost> _serviceTypes;
public ObservableCollection<ServiceCost> ServiceTypes
{
get { return _serviceTypes; }
set
{
if (_serviceTypes != value)
{
_serviceTypes = value;
DynamicOnPropertyChanged();
}
}
}
...
答案 0 :(得分:0)
如果我正确理解您的问题,您只想在XAML中执行此操作。如果是这样,使用EventTrigger
即可。
<ItemsControl.ItemTemplate>
<DataTemplate>
<RadioButton Margin="2,3.5"
Content="{Binding Path=DescriptionPrice}"
GroupName="ServiceType"
IsChecked="{Binding Path=IsSelected}">
<RadioButton.Triggers>
<EventTrigger RoutedEvent="ToggleButton.Checked">
</EventTrigger>
</RadioButton.Triggers>
</RadioButton>
</DataTemplate>
</ItemsControl.ItemTemplate>
请注意,RadioButton的Checked
事件可通过其基类ToggleButton
访问。如果您需要在此活动中调用代码,可以尝试this technique。
答案 1 :(得分:0)
我遇到的问题是在点击单选按钮后访问viewModel。经过大量的搜索,我发现ListBox是一种包装radiobuttons的方式,它有一个selectedItem,然后我可以将列表框的selectedItem绑定到我的ViewModel并有权访问该变量。我不知道这是否是让它工作的最好方法,但它对我有用。
ServiceCost.cs =未更改。
XAML:
...
<GroupBox
Grid.Row="1"
Grid.ColumnSpan="2"
Grid.Column="2"
FontWeight="Bold"
Header="Select Service Type"
Margin="0,7,0,0"
Padding="2" >
<ListBox Margin="0,10,0,10" BorderThickness="0" Background="Transparent"
ItemsSource="{Binding ServiceCosts}"
SelectedItem="{Binding SelectedServiceCost}" >
<ListBox.ItemContainerStyle>
<Style TargetType="{x:Type ListBoxItem}">
<Setter Property="IsSelected" Value="{Binding Path=IsSelected, Mode=TwoWay}" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
<ContentPresenter/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ListBox.ItemContainerStyle>
<ListBox.ItemTemplate>
<DataTemplate>
<RadioButton Content="{Binding DescriptionPrice}"
IsChecked="{Binding Path=IsSelected, Mode=TwoWay, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBoxItem}}}" Margin="0,5,0,5"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</GroupBox>
...
viewModel:
...
private ObservableCollection<ServiceCost> _serviceCosts;
public ObservableCollection<ServiceCost> ServiceCosts
{
get { return _serviceCosts; }
set { if (_serviceCosts != value){_serviceCosts = value; DynamicOnPropertyChanged();}}
}
private ServiceCost _selectedServiceCost;
public ServiceCost SelectedServiceCost
{
get { return _selectedServiceCost; }
set
{
if (_selectedServiceCost != value)
{
// This is what I needed access to: do something here!.
_selectedServiceCost = value;
DynamicOnPropertyChanged();
}
}
}
...