0

I have this style:

                <DataTemplate>
                    <Button
                        Width="44"
                        Height="24"
                        VerticalAlignment="Top"
                        VerticalContentAlignment="Center"
                        HorizontalAlignment="Left"
                        HorizontalContentAlignment="Center"
                        Command="{Binding UninspectedPrintSelectedCommand}"
                        CommandParameter="{Binding RelativeSource={RelativeSource Self}, Path=Content}">

But the command does not work correctly. Looking at the output window yields this issue:

System.Windows.Data Error: 40 : BindingExpression path error: 'UninspectedPrintSelectedCommand' property not found on 'object' ''String' (HashCode=701007577)'. BindingExpression:Path=UninspectedPrintSelectedCommand; DataItem='String' (HashCode=701007577); target element is 'Button' (Name=''); target property is 'Command' (type 'ICommand')

ViewModel ICommand Property:

    public ICommand UninspectedPrintSelectedCommand
    {
        get
        {
            return new DelegateCommand<object>((print) =>
            {
                string printName = print.ToString();
                int indexOfX = printName.IndexOf('x');
                Row = DiePrint.GetRow(printName);
                Col = DiePrint.GetCol(printName);

                if (diePrint == null) { diePrint = new DiePrint(Row + "x" + Col); }
                else
                {
                    diePrint.Row = Convert.ToInt32(row);
                    diePrint.Col = Convert.ToInt32(col);
                }
                LoadMap();
            });
        }
    }

I have no idea how to resolve this. How is the Command property of the Button being interpreted as a String?

If it means anything, this is in my App.xaml file, not the main window.

kformeck
  • 1,703
  • 5
  • 22
  • 43
  • Code for the property? – Matthew Frontino May 27 '15 at 15:48
  • You are binding to the `DataContext` of the `Button`, which is going to be set to the object used to instantiate the the view associated with the `DataTemplate`. Based on the output, the DataTemplate must be being used for a string. – Xavier May 27 '15 at 15:51
  • @MatthewFrontino I added the view model's ICommand property that I am trying to bind to in the question – kformeck May 27 '15 at 15:52
  • I don't think the issue is in the command or the data template itself (although sending the content of a button to a command seems a little odd.). I think it is most likely in how the data template is being used. – Xavier May 27 '15 at 15:55
  • @Xavier, Yes I am binding the ListBox to an ObservableCollection. How can I set up this DataTemplate to use a Button with Command that passes in the Content of the Button to the ICommand property in the view model? – kformeck May 27 '15 at 15:55

1 Answers1

1

I think what you want is to have your binding use a source higher up in your visual tree, where the DataContext is set to the viewmodel you want, rather than being set to a string.

For example, assuming there is a Grid higher up with the right data context, you would use a binding like this:

{Binding DataContext.UninspectedPrintSelectedCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Grid}}}

Replace Grid with something that will reliably be in the visual tree at the right level.

Xavier
  • 3,254
  • 15
  • 22
  • I swapped my Command binding to this and replaced Grid with ListBox (since this Button is the ItemTemplates DataTemplate property of a ListBox) and get this output message: – kformeck May 27 '15 at 16:31
  • BindingExpression path error: 'DiePrintNav' property not found on 'object' ''ListBox' (Name='lbxUninspectedPrints')'. BindingExpression:Path=DiePrintNav.UninspectedPrintSelectedCommand; DataItem='ListBox' (Name='lbxUninspectedPrints'); target element is 'Button' (Name=''); target property is 'Command' (type 'ICommand') – kformeck May 27 '15 at 16:31
  • Sorry, I got the path of the binding slightly wrong, it needs to have `DataContext` in front of the command name. Updated my example. – Xavier May 27 '15 at 16:35
  • Works great now! Thanks. Now I can't seem to get the Content of the Buttons ContentPresenter to be properly passed as a parameter :/ – kformeck May 27 '15 at 16:50
  • scratch that last comment, I got it. Just had to use: – kformeck May 27 '15 at 16:54