5 Replies - 4377 Views - Last Post: 08 June 2013 - 03:16 PM Rate Topic: -----

#1 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5316
  • View blog
  • Posts: 11,357
  • Joined: 02-June 10

Can (how) a DataTemplate reference/use a Style?

Posted 08 June 2013 - 12:52 PM

I'm trying to better grasp MVVM and trying to put it to use in a program just for the sake of really seeing it work. Its a simple little contact list, which is a common practice example.

I would like to have the DataTemplate for my PersonViewModel reference a ResourceDictionary of styles I've defined. That way if I define a TextBlock style for NormalText all I have to do is change that style and it changes everywhere. That's supposed to be one of the beauties of WPF/MVVM, right?

My problem is, I have everything working right up to having the DataTemplate use/reference the styles. I can't figure out the syntax for it. If anyone can point me in the right direction I'd appreciate it. Here's what I have so far.

Attached Image



Person.cs class model
Spoiler


PersonVm.cs ViewModel
Spoiler


PersonDt DataTemplate
Lines 7-10 seem to be where I think I am having the issue figuring out the syntax.

Spoiler


PersonUC UserControl as the View
Spoiler



A Window with the PersonUC on it, binding its DataContext to the collection held in the App
Spoiler


App.xaml.cs holding my collection
Spoiler


And of course the Styles.xaml file
Spoiler

This post has been edited by tlhIn`toq: 08 June 2013 - 12:56 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Can (how) a DataTemplate reference/use a Style?

#2 MrShoes  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 312
  • View blog
  • Posts: 486
  • Joined: 13-June 12

Re: Can (how) a DataTemplate reference/use a Style?

Posted 08 June 2013 - 01:40 PM

This might not be of help to you, but I usually just put styles in the App.xaml file, for example:

<Style TargetType="Label" x:Key="Heading">
            <Setter Property="FontFamily" Value="Arial"/>
            <Setter Property="FontSize" Value="20"/>
            <Setter Property="FontWeight" Value="Bold"/>
            <Setter Property="HorizontalAlignment" Value="Center"/>
            <Setter Property="VerticalAlignment" Value="Center"/>
        </Style>


The just reference it like so:
<Label Style="{StaticResource Heading}">Heading</Label>


Might just be I'm missing what you're trying to do.
Was This Post Helpful? 0
  • +
  • -

#3 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5316
  • View blog
  • Posts: 11,357
  • Joined: 02-June 10

Re: Can (how) a DataTemplate reference/use a Style?

Posted 08 June 2013 - 01:56 PM

Which is what I've done in the past too.
But then you have to copy/paste them from wherever you keep them into the App.xaml. I'm trying to include the styles as a file because then it can just be included as a reference. Which have done successfully as well. I'm just not making that final jump to having the DataTemplate see the style. <hmm....> Will keep beating on it.
Was This Post Helpful? 0
  • +
  • -

#4 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5316
  • View blog
  • Posts: 11,357
  • Joined: 02-June 10

Re: Can (how) a DataTemplate reference/use a Style?

Posted 08 June 2013 - 02:07 PM

AHA.... Like so many things its easy to over think.
You don't add the style file to DataTemplate as a resource.
You just add it to the App file as a resource, then refer to it in the DataTemplate.

So the App.xaml becomes this
Spoiler


Now the PersonDT.xaml datatemplate can do this
Spoiler


I was just over thinking it. In my mind there had to be another step of referencing the style document directly in the DataTemplate.

This post has been edited by tlhIn`toq: 08 June 2013 - 02:13 PM

Was This Post Helpful? 1
  • +
  • -

#5 MrShoes  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 312
  • View blog
  • Posts: 486
  • Joined: 13-June 12

Re: Can (how) a DataTemplate reference/use a Style?

Posted 08 June 2013 - 02:15 PM

Right, I think I get your problem.

As a test, I just made a WPF project with a ResourceDictionary in it (with BuildAction set to Resource. It has a Label style named "CustomLabel".

Then, I made another project with a Window that referenced the ResourceDictionary. I found that I could use the expected <Label Style="{StaticResource CustomLabel}"> in this new project and it applied the style.

EDIT: I see you sorted it. Nice work!

This post has been edited by MrShoes: 08 June 2013 - 02:16 PM

Was This Post Helpful? 0
  • +
  • -

#6 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5316
  • View blog
  • Posts: 11,357
  • Joined: 02-June 10

Re: Can (how) a DataTemplate reference/use a Style?

Posted 08 June 2013 - 03:16 PM

Yep. The difference is that your project isn't going through the whole MVVM pattern, with a DataTemplate that contains the Label or TextBox etc. Once the architecture got more complex I continued to complex-ify it in my head.
:withstupid:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1