locked
Binding source does not work.(Source={StaticResource ViewModel}) RRS feed

  • Question

  • Hi, i am setting datacontext in code behind:

    Snippet

    Snippet
            private PesquisaEnderecoViewModel PesquisaEnderecoViewModel;

            public PesquisaEndereco()
            {
                InitializeComponent();
                PesquisaEnderecoViewModel = new PesquisaEnderecoViewModel();
               this.DataContext = PesquisaEnderecoViewModel;
            }

    But when I try use hightlight:
    Snippet
         <DataTemplate x:Key="HighlightCellTemplate">
                <jeff:HighlightingTextBlock 
                    Style="{StaticResource HighlightTextBlockStyle}" 
                    HighlightText="{Binding SearchText}" >
                    <i:Interaction.Behaviors>
                        <local:CellValueBindingBehavior />
                    </i:Interaction.Behaviors>
                </jeff:HighlightingTextBlock>
            </DataTemplate>

    does not work, all other things works fine(combobox binds), but only this thing does not work,
    work only set vm in xaml such:
    Snippet
    <vm:PesquisaEnderecoViewModel x:Key="ViewModel" /> 

    and

    Snippet
     <DataTemplate x:Key="HighlightCellTemplate">
                <Highlighting:HighlightingTextBlock 
                    Style="{StaticResource HighlightTextBlockStyle}" 
                    HighlightText="{Binding SearchText, Source={StaticResource ViewModel}}" >
                    <i:Interaction.Behaviors>
                        <local:CellValueBindingBehaviorHelper />
                    </i:Interaction.Behaviors>
                </Highlighting:HighlightingTextBlock>
            </DataTemplate>


    anybody can help me?



    Tuesday, August 31, 2010 2:35 PM

Answers

  • Hi,

    As you know, it works like this (You set the data source explicitly):

    HighlightText="{Binding SearchText, Source={StaticResource ViewModel}}" >

     

    I guess your DataTemplate is for the Item Control, so I think you should set the value for the control likes this.

    ItemsSource={Binding}

     

    Hope it will help you.

     

    Regards

    Tuesday, September 7, 2010 5:32 AM

All replies

  • Only thing is I'd check the DataContext of the HighlightCellTemplate after setting it on the page/usercontrol. maybe it is not getting it right... or it is not properly initialized...

    Wednesday, September 1, 2010 5:17 AM
  • hmm..i thing this right, with VM in XAML works fine. 

    <vm:PesquisaEnderecoViewModel x:Key="ViewModel" /> 
    but i want set datacontext in code-behind:
    PesquisaEnderecoViewModel = new PesquisaEnderecoViewModel();
    this.DataContext = PesquisaEnderecoViewModel;
    and highlight does not work when i set datacontext in xaml


    Wednesday, September 1, 2010 8:55 AM
  • this is not simple,to solve?

    Monday, September 6, 2010 7:51 AM
  • Hi,

    As you know, it works like this (You set the data source explicitly):

    HighlightText="{Binding SearchText, Source={StaticResource ViewModel}}" >

     

    I guess your DataTemplate is for the Item Control, so I think you should set the value for the control likes this.

    ItemsSource={Binding}

     

    Hope it will help you.

     

    Regards

    Tuesday, September 7, 2010 5:32 AM