Note that there are some explanatory texts on larger screens.

plurals
  1. POBinding Silverlight UserControl custom properties to its' elements
    primarykey
    data
    text
    <p>I'm trying to make a simple crossword puzzle game in Silverlight 2.0. I'm working on a UserControl-ish component that represents a square in the puzzle. I'm having trouble with binding up my UserControl's properties with its' elements. I've finally (sort of) got it working (may be helpful to some - it took me a few long hours), but wanted to make it more 'elegant'.</p> <p>I've imagined it should have a compartment for the content and a label (in the upper right corner) that optionally contains its' number. The content control probably be a TextBox, while label control could be a TextBlock. So I created a UserControl with this basic structure (the values are hardcoded at this stage):</p> <pre><code> &lt;UserControl x:Class="XWord.Square" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" FontSize="30" Width="100" Height="100"&gt; &lt;Grid x:Name="LayoutRoot" Background="White"&gt; &lt;Grid.ColumnDefinitions&gt; &lt;ColumnDefinition Width="*"/&gt; &lt;ColumnDefinition Width="Auto"/&gt; &lt;/Grid.ColumnDefinitions&gt; &lt;Grid.RowDefinitions&gt; &lt;RowDefinition Height="Auto"/&gt; &lt;RowDefinition Height="*"/&gt; &lt;/Grid.RowDefinitions&gt; &lt;TextBlock x:Name="Label" Grid.Row="0" Grid.Column="1" Text="7"/&gt; &lt;TextBox x:Name="Content" Grid.Row="1" Grid.Column="0" Text="A" BorderThickness="0" /&gt; &lt;/Grid&gt; &lt;/UserControl&gt; </code></pre> <p>I've also created DependencyProperties in the Square class like this:</p> <pre><code> public static readonly DependencyProperty LabelTextProperty; public static readonly DependencyProperty ContentCharacterProperty; // ...(static constructor with property registration, .NET properties // omitted for brevity)... </code></pre> <p>Now I'd like to figure out how to bind the Label and Content element to the two properties. I do it like this (in the code-behind file):</p> <pre><code> Label.SetBinding( TextBlock.TextProperty, new Binding { Source = this, Path = new PropertyPath( "LabelText" ), Mode = BindingMode.OneWay } ); Content.SetBinding( TextBox.TextProperty, new Binding { Source = this, Path = new PropertyPath( "ContentCharacter" ), Mode = BindingMode.TwoWay } ); </code></pre> <p>That would be more elegant done in XAML. Does anyone know how that's done?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload