Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Formatting an element contained in another control
Message
From
09/03/2008 13:47:00
 
 
General information
Forum:
ASP.NET
Category:
Windows Presentation Foundation (WPF)
Environment versions
Environment:
C# 3.0
OS:
Windows XP SP2
Network:
Windows 2000 Server
Database:
MS SQL Server
Miscellaneous
Thread ID:
01300068
Message ID:
01300227
Views:
7
>>Still feeling brain dead on XAML :(
>>
>>Simple control:
>>
>>
>><UserControl x:Class="cdsThemes.TestControl2"
>>    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
>>    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
>>    Height="300" Width="300">
>>    <Grid Background="BlanchedAlmond">
>>  <TextBox Height="23"
>>             Margin="25,103,0,0"
>>             Name="textBox1"
>>             VerticalAlignment="Top"
>>             HorizontalAlignment="Left"
>>             Width="120">Blah Blah Blah</TextBox>
>>  </Grid>
>></UserControl>
>>
>>And I use it in another simple control:
>>
>>
>><UserControl x:Class="cdsThemes.TestControl1"
>>    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
>>    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
>>    xmlns:local="clr-namespace:cdsThemes"
>>    Height="300" Width="300">
>>    <Grid>
>>    <local:TestControl2 x:Name="testControl2"></local:TestControl2>
>>  </Grid>
>></UserControl>
>>
>>
>>Lets say I want to change the margin on textBox1 inside of TestControl2 from the XAML in TestControl1. How do I do that?
>>
>>My actual situation may be harder because textBox1 in that case is a custom control based on a TextBox defined in another assembly. But I can't even get this one to work, so I figured I'd start with the simpler problem.
>>
>>I can do it easy in C#
>>
>>
>>testControl2.textBox1.Margin = new Thickness(1, 1, 1, 1);
>>
>>
>>just can't seem to work out the XAML.
>
>For a rough and ready solution you can create a public property on the contained control. e.g:
>public Thickness TextBoxMargin
>        {
>            get { return textBox1.Margin; }
>            set { textBox1.Margin = value; }
>        }
>
Then, in containing controls XAML you can use:
>    <local:TestControl2 x:Name="testControl2" TextBoxMargin="1,1,1,1"></local:TestControl2>
But I think creating a DependencyProperty is the 'correct' answer.
>HTH,
>Viv

I spent a bunch more time googling last night and came to same conclusion.

I would have thought that you could do it directly in XAML, but it looks like you can't. It takes some codebehind work to make it happen.

It's going to take me a bit to fully get the concept of dependency properties. This helped:

http://joshsmithonwpf.wordpress.com/2007/05/16/demystifying-dependency-properties/

But it's still going to take me a bit to understand the ramifications and the usefulness of a property that ignores you some of the time.

Thanks!
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform