开发者

Silverlight XAML: Referencing Code-behind class

Assuming the following view model definition:

public class MyObject {
public string Name { get; set; }

}

public interface IMyViewModel 开发者_开发知识库{ 
ICommand MyCommand { get; }
IList<MyObject> MyList { get; }

}

And a UserControl with the following code behind:

public class MyView : UserControl {
public IMyViewModel Model { get; }

}

If my XAML looked like this:

<UserControl>
<ListBox ItemsSource="{Binding MyList}">
    <ListBox.ItemTemplate>
        <TextBlock Text="{Binding Name}" />
        <Button Content="Execute My Command" cmd:Click.Command="{Binding Path=MyCommand, ?????????}" cmd:Click.CommandParameter="{Binding}" />
    </ListBox.ItemTemplate>
</ListBox>

How can I bind my Button to the ICommand property of my code-behind class?

I'm using Prism and SL 3.0 and I need to bind each button in my list box to the same command on my view model.

Before my UserControl had a specific name and I was able to use the ElementName binding, but now my UserControl is used multiple times in the same parent view so I can't use that technique anymore and I can't figure out how to do this in XAML.

If it is my only option I can do it manually in the code-behind, but I'd rather do it declaratively in the XAML, if possible.


You need a DataContextProxy for this to work because you're no longer in the context of the UserControl. You've moved out of that and there is no good way to reach back into that context without something like the DataContextProxy. I've used it for my projects and it works great.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜