none
how to binding commandparameter to UIelement with out using ElementName?

    Question

  • Hi,

    I want to use data binding to bind a UIElement as a commandparameter, the only way I cn do this is to use the elementName.

    I want to know if there is another way of doing the same thing without using the elementName?(get rid of "ElementName=myMediaElement")

    Code follows:

    <MediaElement x:Name="myMediaElement" />
    
    <Button HorizontalAlignment="Center" Width="30" Background="Transparent" 
    
        Command="{Binding CaptureCommand}" 
    
    CommandParameter="{Binding ElementName=myMediaElement}"
    
    >
    
    

    cheers

     

    cooper

    Friday, October 08, 2010 6:24 AM

Answers

  • Hi Cooper1983,

    Would you please tell us why you want to get rid of elementname? It's hard to make any suggestions without knowing how to locate this MediaElement.

    Generally, you can refer to http://msdn.microsoft.com/en-us/library/ms752347.aspx for all possible ways to do a binding.

    Hope this helps.

    Best regards,

    Min


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
    Tuesday, October 12, 2010 6:36 AM

All replies

  • if you want to send Name of the element as CommandParamenter then give Path=Name

     

    <MediaElement x:Name="myMediaElement" >
    <Button HorizontalAlignment="Center" Width="30" Background="Transparent" 
        Command="{Binding CaptureCommand}" 
    CommandParameter="{Binding ElementName=myMediaElement, Path=Name}"
    >
    
    

    Friday, October 08, 2010 6:58 AM
  • Hi Cooper,

    in your case, you could go for a RelativeSource , i.e. {Binding RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type MediaElement}} .


    Cheers,
    Olaf
    http://blogs.intuidev.com
    Friday, October 08, 2010 9:19 AM
  • I don't think RelativeSource/FindAncestor will help in his case because the MediaElement and the Button where he wants to do the binding are peers.  Cooper, why don't you want to use ElementName?
    Friday, October 08, 2010 6:06 PM
  • Hi Cooper1983,

    Would you please tell us why you want to get rid of elementname? It's hard to make any suggestions without knowing how to locate this MediaElement.

    Generally, you can refer to http://msdn.microsoft.com/en-us/library/ms752347.aspx for all possible ways to do a binding.

    Hope this helps.

    Best regards,

    Min


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
    Tuesday, October 12, 2010 6:36 AM
  • Would you please tell us why you want to get rid of elementname? It's hard to make any suggestions without knowing how to locate this MediaElement.

    Because ElementName doesn't seem to work.

    <StackPanel>
        <TextBox Name="myMediaElement" />
    </StackPanel>
    <StackPanel Grid.Column="1" Grid.RowSpan="2">
        <Button HorizontalAlignment="Right" Content="Select" Command="{Binding ViewOtherJobsCommand}" CommandParameter="{Binding ElementName=myMediaElement, Path=Text}" />
    </StackPanel>

    System.Windows.Data Error: 4 : Cannot find source for binding with reference 'ElementName=myMediaElement'. BindingExpression:Path=Text; DataItem=null; target element is 'Button' (Name=''); target property is 'CommandParameter' (type 'Object')

    My DataContext is set on my root grid.

    I'm not using any Templates. These are just plain controls, with ICommand implemented for the Command.

    Thursday, November 15, 2012 6:01 AM