【问题标题】:Unable to cast object of type 'System.Windows.Data.Binding' to type 'System.String无法将“System.Windows.Data.Binding”类型的对象转换为“System.String”类型
【发布时间】: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}" /&gt; 而抛出,我想知道我是否可以在尝试时使用中继命令来完成此操作。谢谢

【问题讨论】:

    标签: c# wpf mvvm


    【解决方案1】:

    您的中继命令是&lt;string&gt; 类型的泛型,因此它试图将绑定从命令参数转换为字符串。

    此处的内容将是一个框架元素,由当时可用的 DataTemplates 确定。

    将中继命令设为RelayCommand&lt;object&gt;。在中继命令的匿名方法中,x.GetType 将告诉您那里设置的内容是什么。

    希望这能消除您的疑虑。

    【讨论】:

      猜你喜欢
      • 2011-08-31
      • 2012-09-07
      • 2014-03-14
      • 2017-11-24
      • 1970-01-01
      • 1970-01-01
      • 2021-04-26
      相关资源
      最近更新 更多