Wpf for Silverlight xaml

Ok I have a ResourceDictionary definition that I used for the WPF application below:

<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

<Style x:Key="fieldsPanel" TargetType="{x:Type StackPanel}">
    <Style.Resources>
        <Style TargetType="{x:Type CheckBox}">
            <Setter Property="Margin" Value="8 2" />
        </Style>
        <Style TargetType="{x:Type TextBlock}">
            <Setter Property="Margin" Value="0 2" />
            <Setter Property="Width" Value="100" />
        </Style> 
    </Style.Resources>
</Style>

      

This exact block of XAML brought into my Silverlight application shows 4 errors in the error bar (first error appears 3 times, second time)

I can remove all instances of the first error by replacing the text "{x: Type TypeName}" with "TypeName". But is this the correct way to fix it? Is this just another random and arbitrary difference between WPF and Silverlight?

But I cannot figure out what I am doing wrong in the second error. Can anyone help me (and the place where you found the answer)?

EDIT:

If I knew how to Google "Style.Resources" (so Google won't ignore the dot), or if I knew the terminology for what's called this type of construction, I think I could figure it out myself.

EDIT:

It turns out that what I am trying to accomplish is perhaps best done with a UserControl. But apparently I need to learn a whole new set of rules for creating UserControls in Silverlight.

Long story short, I want something like this:

<CheckBox Click="myClickHandler"  Name="myName"/><TextBlock Text="The label for the checkbox" />

      

... contributed to a UserControl that I can create multiple times with different names and shortcuts. All checkboxes will use the same click handler.

If anyone wants to hack some quick UserControl code and post it, I'd appreciate it. If not (and I totally understand that I don't want to do free work for someone else), I ran Googling.

+2


source to share


2 answers


At first glance, this seems to be the correct way to fix the problem, and it seems like "another random and arbitrary difference" between WPF and Silverlight. However, making such a change does not actually create code that works.

As Clay correctly points out in his latest reign, he really needs to make a UserControl with Dependency Properties for variable parts (text, etc.).

Here's the ResourceDictonary page on MSDN which has more information on the format.

Remember, Silverlight is not a subset of WPF. Microsoft had to completely rewrite the code that launches Silverlight applications in order for a small load to work.

EDIT



Required XAML:

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:MyControls">
    >
    <Style TargetType="StackPanel">
        <Setter Property="Template">
            <Setter.Value>
                <Control Template TargetType="local:myPanel">
                    <CheckBox x:Name="myCheckBox"
                              Content="{TemplateBinding CheckBoxLabel}"
                              Margin="8,0,2,0" Width = "100"/>
                </Control>
            </Setter.Value>
         </Setter>    
    </Style>
</ResourceDictionary>

      

Then you need to create a cs file containing the code for your custom control:

namespace MyControls
{
    public class myPanel : ContentControl
    {
        public myPanel()
        {
            DefaultStyleKey = typeof(myPanel);
        }
    }

    public override void OnApplyTemplate()
    {
        base.OnApplyTemplate();

        CheckBox checkbox = GetTemplateChild("myCheckBox") as CheckBox;
        checkbox.Click += CheckBoxClick;
    }

    void CheckBoxClick(object sender, RoutedEventArgs e)
    {
        CheckBox checkbox = sender as CheckBox;
        // Do this CheckBox specific stuff
    }

    public string CheckBoxLabel
    {
        get { return (string)GetValue(CheckBoxLabelProperty); }
        set { SetValue(CheckBoxLabelProperty, value); }
    }

    public static readonly DependencyProperty CheckBoxLabelProperty =
        DependencyProperty.Register("CheckBoxLabel", typeof(string),
                                    typeof(myPanel), null);
 }

      

Disclaimer - I just assembled this from a more complex custom control, so there is no guarantee it will work out of the box. You may have to tweak it a bit to get it to work 100%.

+2


source


I'm not sure if I would bother creating a new control as no new functionality is required. In the very worst case, you may need to create a completely new template for the CheckBox if you really want to go to the city of your own. However, I doubt you even need to touch the template in this case.

The property Padding

is rendered as a property Margin

of ContentPresenter

the default internal template. If you must have a label width of exactly 100 pixels, you can set the width of the checkbox control to 116 (the checkbox itself is 16 pixels).

Hence your desired visuals can be approached like this: -



<CheckBox x:Name="myName" Content="The label for the checkbox"
  Margin="8 2 0 2" Padding="8 0 0 0" Width="116" />

      

One key difference from SL versus WPF is that you cannot override the style for Type locally. You need to bind the Style property of the control to the resource: -

<UserControl .... blah, blah>
   <UserControl.Resources>
       <Style x:Key="MyCheckBoxes" TargetType="CheckBox">
           <Setter Name="Margin" Value="8 2 0 2" />
           <Setter Name="Padding" Value="8 0 0 0" />
           <Setter Name="Width" Value="116" />
       </Style>
   </UserControl.Resources>
  <CheckBox x:Name="myName" Content="The label for the checkbox"
    Style="{StaticResource MyCheckBoxes}" />

      

+1


source







All Articles