Reputation: 6047
Is it possible to bind data using Type-Qualified (see Single Property, Attached or Otherwise Type-Qualified
section) syntax in WinRT?
What I want to get is to have possibility to bind to an item of my ViewModel which is an interface:
public interface IViewModel {
INewsContainer ItemHost {get;}
}
public interface INewsContainer {
ObservableCollection<INews> News {get;}
}
class ViewModel: IViewModel, INewsContainer {
// ....
public INewsContainer ItemHost { get { return this; } }
// ...
ObservableCollection<INews> news;
ObservableCollection<INews> INewsContainer.News { get { return news; } }
}
Normally, in WPF binding like the following one works fine (assuming DataContext
is an instance of ViewModel
):
<ListView Grid.Column="1"
ItemsSource="{Binding Path=ItemHost.(vm:INewsContainer.News)}" />
But if I try doing so in WinRT it fails with log in Immediate Window:
A first chance exception of type 'Windows.UI.Xaml.Markup.XamlParseException' occurred (...) Failed to assign to property 'Windows.UI.Xaml.Data.Binding.Path'. [Line: 35 Position: 17]
"Regular" binding, i.e. Path=ItemHost.News
doesn't work either. It states that News
property cannot be found in an instance of class ViewModel.
Workaround
This workaround works fine but I really hate having a converter over here :(
Upvotes: 2
Views: 85
Reputation: 31831
If you want to do that, you need to implement the interface both explicitly and implicitly.
Like this:
public interface IViewModel
{
string Name { get; set; }
}
public class ViewModel : IViewModel
{
public ViewModel()
{
(this as IViewModel).Name = "Jerry";
}
public string Name
{
get { return (this as IViewModel).Name; }
set { (this as IViewModel).Name = value; }
}
string IViewModel.Name { get; set; }
}
Then you can do this
<Grid Background="Black">
<Grid.DataContext>
<local:ViewModel/>
</Grid.DataContext>
<TextBlock Text="{Binding Name}" />
</Grid>
Read: http://msdn.microsoft.com/en-us/library/aa288461(v=vs.71).aspx
Best of luck.
Upvotes: 1