A datatemplate can be used when the scenario is fairly simple, and where you
are trying to reuse the presentation / layout of a set of data, but not if you
are trying to reuse the behaviour and code behind.
If you want to build something reusable that behaves in a complex manner then
it probably won't be suit. Keep in mind that you can achieve complexity
as long it is abstracted to what you bind to, such as your viewmodel.
Maybe this is a good thing?
You can use a DataTemplate independently of things such as a ListBox, by using
a ContentControl.
That way, you could define your datatemplate as an application wide resource,
then use/bind to it where ever you wish to display
Here is a simple example, where the datatemplate and host control both are
bound to the same :
<Grid x:Name="LayoutRoot">
<Grid.Resources>
<DataTemplate
x:Name="dtTest">
<StackPanel
Orientation="Horizontal">
<TextBlock Text="Data Template:" Foreground="Blue"/>
<TextBox
Text="{Binding Path=TextTest,Mode=TwoWay}"
/>
</StackPanel>
</DataTemplate>
</Grid.Resources>
<Grid.RowDefinitions>
<RowDefinition Height="100"/>
<RowDefinition Height="100"/>
</Grid.RowDefinitions>
<ContentControl ContentTemplate="{StaticResource dtTest}"
x:Name="ccTest" Grid.Row="0" Margin="10,5,0,0">
<ContentPresenter/>
</ContentControl>
<StackPanel Grid.Row="1" Orientation="Horizontal"
Margin="10,5,0,0">
<TextBlock Text="Parent control:"
Foreground="Pink" VerticalAlignment="Top"/>
<TextBox Text="{Binding TextTest,
Mode=TwoWay}" Height="25" Width="100" HorizontalAlignment="Left"
VerticalAlignment="Top" />
</StackPanel>
</Grid>
public MainPage()
{
InitializeComponent();
this.DataContext =
this;
}
private string _textTest;
public string TextTest
{
get { return
_textTest; }
set {
_textTest = value; OnPropertyChanged(new PropertyChangedEventArgs("TextTest"));
}
}
On Mon, 25 Jan 2010 07:18:04 +0530 wrote
>yes I can and do set viewmodel properties. But again, they only work in the
code-behind.
>
>Your use of data templates is a bit more interesting. Doesn't that just
work as part of particular
>controls?
>
>T.
>
>On Mon, Jan 25th, 2010 at 12:04 PM, Vishwanath Humpy
<[email protected]> wrote:
>
>> Sorry you probably know this already, but I'll say it.
>>
>> You are grabbing a reference to the viewmodel in your code behind
>> already, can't you create a public method on the child control
>> eg public void SetViewModel(PriceViewModel model) and just set
>> it at the same time as the parent :
>>
>> moPriceViewModel = Resources["PriceViewModel"] as PriceViewModel;
>> childControl.SetViewModel(moPriceViewModel);
>>
>> Ideally I don't like this, as it introduces a tight coupling.
>> However, if the child control is searching the visual tree for
>> resources, you already have a coupling, but it is less obvious to a
>> consumer of the control. At least the SetViewModel approach
>> documents the incoming interface of your control.
>>
>> But UserControls are annoying like this. I prefer to use
>> DataTemplates in simple scenarios, as they can refer to parent
>> bindings.
>>
>> And if the richness of a user control is required, I like to
>> initialize as above.
>>
>> I find the UserControl a little annoying, as it:
>>
>> 1. Usually breaks in Blend.
>> 2. Difficult to share datacontext without explicit
>> initialization in code behind.
>> 3. DependencyProperty can't be used very easily for complex
>> scenarios, as it is difficult to propagate change
>> notifications. The changed event handler for dependency
>> property register is static making it unuseful for an instance of a
>> control, and property changed doesn't seem to be caught be bindings
>> when fired from a dependency property setter.
>> .....
>> ....
>>
>> I would apperciate if I could do the following :
>>
>> 1. Create a usercontrol, MyUserControl, with a dependency
>> property called ViewModel.
>> 2. Place MyUserControl on a parent control, and bind to a
>> property of the parent control called PriceViewModel:
>>
>> <local:MyUserControl x:Name="myUserControl1" ViewModel = "{Binding
>> PriceViewModel}" />
>>
>> However, I think there is a race condition, where the binding happens
>> on InitializeComponent, therefore PriceViewModel is null. Even
>> if this is solved, in the setter of the ViewModel dependency property
>> we run into trouble trying to force bindings to it to be notified as
>> I complained above.
>>
>> Maybe I don't understand, why can't I implement with this approach?
>>
>>
>>
>> On Mon, 25 Jan 2010 05:16:15 +0530 wrote
>> >Hi,
>> >
>> >Apologies for the change of title - I don't have access to the
>> original emails. Thanks to the people
>> >who have replied so far.
>> >
>> >I am still having problems with trying to access page resources
>> from a user control.
>> >
>> >If I put this withing the control:
>> >
>> >
>> >
>> >
>> >where vm aliases the namespace:
>> xmlns:vm="clr-namespace:MyCustomer.MyProject.ViewModel"
>> >
>> >Then I can access the ViewModel object, however it's a newly
>> instantiated instance of that view
>> >model object.
>> >
>> >My preference is to reuse the existing one that exists on the
>> parent Page, as that one already has
>> >data loaded that I want to use, hence less unnecessary database
>> requests.
>> >
>> >fyi, the price view model object is declared in the xaml as:
>> >
>> >
>> >
>> >
>> >This exists in the constructor of the page (after
>> InitializeComponent), and, as expected, is not null.
>> >So the following line
>> >moPriceViewModel = Resources["PriceViewModel"] as
>> PriceViewModel;
>> >is set correctly.
>> >
>> >Adding the DataContext did nothing. I added it in the xaml in
>> both the page and in the control root
>> >element, then I tried adding the view model object in the code
>> behind on the parent page, but it
>> >still came through null.
>> >
>> >The datatype of the Parent property is DependencyObject, and it
>> is null when I attempt to access it
>> >from within the constructor of the control (after the
>> InitializeComponent statement), even though
>> >the control exists in the xaml of the parent. Help suggests that
>> I need to set the Parent property by
>> >adding the control to a collection. I did that explicitly
>> (although I believe it should already be set
>> >within the xaml), adding it to the layout grid, but Parent still
>> came through null.
>> >
>> >I also tried
>> >ManageTermsPage mtp =
>> (ManageTermsPage)VisualTreeHelper.GetParent(this);
>> >but I think this failed for the same reason - Parent property is
>> null.
>> >
>> >If I could get the DataContext working, passing the datasource
>> through from the page to the
>> >control, I would be happy with that.
>> >
>> >I do need access to the resource from within the xaml and in the
>> code-behind.
>> >
>> >Any other ideas on how I might achieve this?
>> >
>> >Regards,
>> >Tony
>> >
>> >_______________________________________________
>> >ozsilverlight mailing list
>> >[email protected]
>> >http://prdlxvm0001.codify.net/mailman/listinfo/ozsilverlight
>> >
>
>
>
>_______________________________________________
ozsilverlight mailing list
[email protected]
http://prdlxvm0001.codify.net/mailman/listinfo/ozsilverlight