Click here to Skip to main content
15,911,035 members
Home / Discussions / WPF
   

WPF

 
AnswerRe: Error: Class not registered (Exception from HRESULT: 0x80040154) Pin
thatraja11-Dec-11 19:52
professionalthatraja11-Dec-11 19:52 
QuestionWPF Radio Button Group Behavior Pin
Eric Woodruff10-Dec-11 15:19
professionalEric Woodruff10-Dec-11 15:19 
AnswerRe: WPF Radio Button Group Behavior Pin
SledgeHammer0110-Dec-11 17:51
SledgeHammer0110-Dec-11 17:51 
Questionhow to hide particular grid. Pin
SRKSHOME9-Dec-11 5:41
SRKSHOME9-Dec-11 5:41 
AnswerRe: how to hide particular grid. Pin
Abhinav S9-Dec-11 23:15
Abhinav S9-Dec-11 23:15 
QuestionDataGrid databinding to List<> properties of an object Pin
Member 84745799-Dec-11 3:21
Member 84745799-Dec-11 3:21 
AnswerRe: DataGrid databinding to List properties of an object Pin
Wayne Gaylard9-Dec-11 5:17
professionalWayne Gaylard9-Dec-11 5:17 
QuestionDependencyProperty doesn't find items in Resource Dictionary Pin
Member 76693458-Dec-11 7:46
Member 76693458-Dec-11 7:46 
Dear experts,

Currently i'm trying to mimic the behavior of the ContentControl Control in a FlowDocument environment. I like the ContentControl because it allows to display content in a different template based on the content's type. E.g.:

C#
<ContentControl Content={Binding}/>
will use DataTemplates defined higer up in the XAML in a ResourceDictionary:
<DataTemplate DataType={x:Type local:Person}>...</DataType>
<DataTemplate DataType={x:Type local:Pet}>...</DataType>


I already found Create Flexible UIs With Flow Documents And Data Binding. As a start I altered the ItemsContent as follows:

C#
public class TemplatedContent : Section
    {
        private static readonly DependencyProperty ContentProperty = DependencyProperty.Register("Content", typeof(object), typeof(TemplatedContent), new PropertyMetadata(OnContentChanged));
        private static readonly DependencyProperty TemplateProperty = DependencyProperty.Register("Template", typeof(DataTemplate), typeof(TemplatedContent), new PropertyMetadata(OnTemplateChanged));

        public TemplatedContent()
        {
            //Helpers.FixupDataContext(this);
            Loaded += TemplatedContent_Loaded;
        }

        private void TemplatedContent_Loaded(object sender, RoutedEventArgs e)
        {
            GenerateContent(Template, Content);
        }

        public object Content
        {
            get { return GetValue(ContentProperty); }
            set { SetValue(ContentProperty, value); }
        }

        public DataTemplate Template
        {
            get { return (DataTemplate)GetValue(TemplateProperty); }
            set { SetValue(TemplateProperty, value); }
        }


        private void GenerateContent(DataTemplate template, object content)
        {
            Blocks.Clear();
            if (template != null && content != null)
            {
                FrameworkContentElement element = Helpers.LoadDataTemplate(template);
                element.DataContext = content;
                //Helpers.UnFixupDataContext(element);
                Blocks.Add(Helpers.ConvertToBlock(content, element));
            }
        }

        private void GenerateContent()
        {
            GenerateContent(Template, Content);
        }

        private void OnContentChanged(object newValue)
        {
            if (IsLoaded)
                GenerateContent(Template, newValue);
        }

        private void OnTemplateChanged(DataTemplate newValue)
        {
            if (IsLoaded)
                GenerateContent(newValue, Content);
        }

        private static void OnContentChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ((TemplatedContent)d).OnContentChanged(e.NewValue);
        }

        private static void OnTemplateChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ((TemplatedContent)d).OnTemplateChanged((DataTemplate)e.NewValue);
        }
    }

As you can see it's a verly basic peace of code and it works as expected when i use it in the following manner:
C#
<FlowDocument>
	<local:TemplatedContent Content={Binding}>
	 <local:TemplatedContent.Template>
			<DataTemplate DataType={x:Type local:Person}>...</DataTemplate>
	 </local:TemplatedContent.Template>
	</local:TemplatedContent>
</FlowDocument>


This is all well but to support multiple templates for different content types i will need to define the DataTemplates in a resource dictionary:
C#
<FlowDocument>
	<local:TemplatedContent Content={Binding}>
	</local:TemplatedContent>
</FlowDocument>


and higer up in a resource dictionary:
C#
<DataTemplate DataType={x:Type local:Person}>...</DataTemplate>

Now, the TemplatedContent is unable to find the DataTemplate. How is this possible? If I understand the theory around DependencyProperty correctly it should look in the xaml tree for <datatemplate> entries that match the type of the content right? It doesn't. When setting a breakpoint on line:

private static void OnTemplateChanged

it's never called.


I hope you experts can help me further with this!

Lots of thanks in advance!

edit: sorry for the layoutbugs, i hope it's fixed now

modified 8-Dec-11 14:13pm.

QuestionHow to make muti-geometryModel3D to make different colors in trangle Pin
derek09198-Dec-11 2:51
derek09198-Dec-11 2:51 
Questionhow to draw multi-lines in WPF Pin
Hamed Metalgear6-Dec-11 22:10
Hamed Metalgear6-Dec-11 22:10 
AnswerRe: how to draw multi-lines in WPF Pin
Wayne Gaylard6-Dec-11 22:34
professionalWayne Gaylard6-Dec-11 22:34 
GeneralRe: how to draw multi-lines in WPF Pin
Hamed Metalgear6-Dec-11 22:40
Hamed Metalgear6-Dec-11 22:40 
GeneralRe: how to draw multi-lines in WPF Pin
Wayne Gaylard6-Dec-11 22:44
professionalWayne Gaylard6-Dec-11 22:44 
GeneralRe: how to draw multi-lines in WPF Pin
Hamed Metalgear6-Dec-11 22:46
Hamed Metalgear6-Dec-11 22:46 
GeneralRe: how to draw multi-lines in WPF Pin
Pete O'Hanlon6-Dec-11 23:17
mvePete O'Hanlon6-Dec-11 23:17 
QuestionTreeView Data Template & Binding Question Pin
Kevin Marois6-Dec-11 12:44
professionalKevin Marois6-Dec-11 12:44 
AnswerRe: TreeView Data Template & Binding Question Pin
Mycroft Holmes6-Dec-11 13:52
professionalMycroft Holmes6-Dec-11 13:52 
GeneralRe: TreeView Data Template & Binding Question Pin
SledgeHammer016-Dec-11 13:55
SledgeHammer016-Dec-11 13:55 
GeneralRe: TreeView Data Template & Binding Question Pin
Mycroft Holmes6-Dec-11 14:00
professionalMycroft Holmes6-Dec-11 14:00 
GeneralRe: TreeView Data Template & Binding Question Pin
SledgeHammer016-Dec-11 14:17
SledgeHammer016-Dec-11 14:17 
GeneralRe: TreeView Data Template & Binding Question Pin
Mycroft Holmes6-Dec-11 14:21
professionalMycroft Holmes6-Dec-11 14:21 
GeneralRe: TreeView Data Template & Binding Question Pin
SledgeHammer016-Dec-11 14:25
SledgeHammer016-Dec-11 14:25 
GeneralRe: TreeView Data Template & Binding Question Pin
Mycroft Holmes6-Dec-11 16:22
professionalMycroft Holmes6-Dec-11 16:22 
GeneralRe: TreeView Data Template & Binding Question Pin
Kevin Marois6-Dec-11 14:08
professionalKevin Marois6-Dec-11 14:08 
GeneralRe: TreeView Data Template & Binding Question Pin
Mycroft Holmes6-Dec-11 14:18
professionalMycroft Holmes6-Dec-11 14:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.