【发布时间】:2015-06-06 03:42:56
【问题描述】:
您好,我收到此错误
对匹配指定绑定约束的类型“System.Windows.Data.Binding”的构造函数调用引发异常。 {“无法将“System.Windows.Data.Binding”类型的对象转换为“System.String”类型。”}
我在下面有一个单选按钮类
public class GroupedList
{
public string Group { get; set; }
public bool IsSelected { get; set; }
}
然后在我的视图模型中,我有一个此类的可观察集合
/// <summary>
/// The <see cref="GroupList" /> property's name.
/// </summary>
public const string GroupListPropertyName = "GroupList";
private ObservableCollection<GroupedList> _groupList = new ObservableCollection<GroupedList>();
/// <summary>
/// Sets and gets the GroupList property.
/// Changes to that property's value raise the PropertyChanged event.
/// </summary>
public ObservableCollection<GroupedList> GroupList
{
get { return _groupList; }
set { Set(GroupListPropertyName, ref _groupList, value); }
}
在我看来,我有一个带有单选按钮的组合框作为模板。 ComboBox ItemSource 设置为 Collection。 Content 和 IsCheckedProperties 设置为类中的两个属性。
<ComboBox Name="groupCombo" ItemsSource="{Binding GroupList}">
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<RadioButton x:Name="GroupButton"
GroupName="Options1"
Content="{Binding Group}"
IsChecked="{Binding IsSelected}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Checked">
<command:EventToCommand Command="{Binding
{Binding RelativeSource={RelativeSource FindAncestor,
AncestorType={x:Type UserControl}}, Path=DataContext.GroupChecked}}"
CommandParameter="{Binding Content,ElementName=GroupButton}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</RadioButton>
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
我添加了事件触发器以尝试访问视图模型中以下中继命令中选中事件的内容属性。
private RelayCommand<string> _groupChecked;
/// <summary>
/// Gets the GroupChecked.
/// </summary>
public RelayCommand<string> GroupChecked
{
get
{
return _groupChecked
?? (_groupChecked = new RelayCommand<string>(
x =>
{
if (!string.IsNullOrWhiteSpace(x))
{
//DoStuff
}
}));
}
}
添加命令后,错误正在发生,我想,但不确定它是否因为这条线CommandParameter="{Binding Content,ElementName=GroupButton}" /> 而抛出,我想知道我是否可以在尝试时使用中继命令来完成此操作。谢谢
【问题讨论】: