0

I have User control which contains TextBox with WaterMark inside

<AdornerDecorator>
                        <TextBox 
                            Height="20" 
                            Margin="10,0" 
                            Grid.Column="0" 
                            Text="{Binding MainCategoryTextBoxValue, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Name="MainCatTextBox">

                            <controls:WatermarkService.Watermark>
                                <TextBlock VerticalAlignment="Center" x:Name="MainCategoryTextBlock"> </TextBlock>
                            </controls:WatermarkService.Watermark>
                        </TextBox>
</AdornerDecorator>

You can see here WatermarkService implementation https://stackoverflow.com/a/836463/1548347

I want take "MainCategoryTextBlock" textblock inside <controls:WatermarkService.Watermark> and set it DataContext to be same like my UserControl DataContext in order to change Watermark text in RunTime from my ViewModel.

I tried to bind "MainCategoryTextBlock" DataContext with RelativeSource to my UserControl DataContext but I didn`t succeed (maybe syntax error - Im not sure).

DataContext="{Binding DataContext, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}" 

Do you have any clue how can I solve it?

Thanks

Community
  • 1
  • 1
Ofir
  • 5,049
  • 5
  • 36
  • 61

2 Answers2

0

You should be setting the data context property of your window to your view model, and bind the text property of your textblock to your view model property.

<TextBlock Text={Binding Path=PropertyOnViewModel} />
failedprogramming
  • 2,532
  • 18
  • 21
  • I can`t use this approach. I set the datacontext of my view from code behined (my applicatiop based on PRISM). If I will create Instance from my View and do what you suggested I will get 2 instances of my ViewModel. – Ofir Mar 05 '13 at 04:38
0

If your UserControl has a name then you can do it like this:

DataContext={Binding ElementName="YourUserControlName", Path=DataContext}

But i can see that you are using your textbox inside an adorner decorator so you can't use FindAncestor in this case because your textbox and your UserControl won't belong to the same visual tree.

Ibrahim Najjar
  • 19,178
  • 4
  • 69
  • 95
  • I tried to give my UserControl Name but I get the next message: – Ofir Mar 04 '13 at 17:21
  • ohh, sorry. Here is the message: Error 2 : The type name 'Views' does not exist in the type 'DataPresenterModule.DataPresenterModule'. Meanwhile i solved the problem in other way but I still curios how can i solve it in this way – Ofir Mar 04 '13 at 19:26
  • @Ofir i can't help you a lot with this information, if your problem has been solved and you still want to talk about my proposed solution then meet me in chat. – Ibrahim Najjar Mar 04 '13 at 20:16