简体   繁体   中英

Unable to cast object of type 'System.Windows.Data.Binding' to type 'System.String

Hello I am receiving this error

The invocation of the constructor on type 'System.Windows.Data.Binding' that matches the specified binding constraints threw an exception. {"Unable to cast object of type 'System.Windows.Data.Binding' to type 'System.String'."}

I have a class below for radio buttons

public class GroupedList
{
    public string Group { get; set; }

    public bool IsSelected { get; set; }        
}

Then In my view model I have a observable collection of this class

/// <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); }
}

In my view I have a combo box with a radio button as a template. The ComboBox ItemSource is set to the Collection. The Content and IsCheckedProperties are set to the two properties in the class.

<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>

I added the event trigger to try and access the content property on the checked event in the below relay command in the view model.

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
                }
            }));
    }
}

After adding the command the error is happening and I think, but not sure that it is throwing becuase of this line CommandParameter="{Binding Content,ElementName=GroupButton}" /> I was wondering if I can accomplish this with the relay command as I am trying. Thanks

Your relay command is a generic of type <string> , hence it is trying to cast the binding from command parameter to a string.

The content here would be a framework element which is determined by available DataTemplates at that point.

Make relay command a RelayCommand<object> . In the anonymous method for relay command the x.GetType will tell you whats the content set there.

Hope this clears your doubts.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM