Kumar
Kumar

Reputation: 11369

WPF Binding how to get object reference

I have a ListView showing a list of Accounts using MVVM & a custom template and working fine Now on clicking the Acct Name, we need to execute a custom action which requires the current Acct object

Is there a way to set the Label.Tag property to the Acct object ?

the xaml def is below env is vs2010 .net 4.0 c#

<ListView Name="lv1" Grid.Column="1" Grid.Row="4"
    ItemsSource="{Binding AccountsList}"
    Background="Transparent" BorderThickness="0">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal">
                <Image Source="/asm1;component/Images/Icons/pdf1.png" Width="12" Height="12" />
                <Label Content="{Binding Name}" Margin="0,0,25,0" 
                    ContextMenu="{x:Null}" Name="lblacctItem" 
                    MouseDoubleClick="lbl_MouseDoubleClick" />
            </StackPanel>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
    <ListView.ItemsPanel>
        <ItemsPanelTemplate>
            <WrapPanel Background="Transparent" 
                Width="{Binding (FrameworkElement.ActualWidth), RelativeSource={RelativeSource AncestorType=ScrollContentPresenter}}" 
                ItemWidth="{Binding (ListView.View).ItemWidth, RelativeSource={RelativeSource AncestorType=ListView}}" 
                MinWidth="{Binding ItemWidth, RelativeSource={RelativeSource Self}}" 
                ItemHeight="{Binding (ListView.View).ItemHeight, RelativeSource={RelativeSource AncestorType=ListView}}" />
        </ItemsPanelTemplate>
    </ListView.ItemsPanel>
</ListView>

Upvotes: 0

Views: 1403

Answers (2)

Tilak
Tilak

Reputation: 30728

You are mixing MVVM and code behind.

You should create a command behavior (Link) to label. Then you should bind CommandParameter to AccObject, and Command to the action you need to execute.

Following should be removed.

MouseDoubleClick="lbl_MouseDoubleClick"

Updated as per comments

As current item is bound to AccObject, simply use Binding in command parameter.

CommandParameter = {Binding}

Upvotes: 2

rajnikant rajwadi
rajnikant rajwadi

Reputation: 36

You can set SelectedItem property to SelectedAccount property of viewmodel.

SelectedItem="{Binding SelectedAccount}" Background="Transparent" BorderThickness="0">

Use INotifyPropertyChanged interface on SelectedAccount property.

Thanks, Rajnikant

Upvotes: 1

Related Questions