3

I'm writing tests using WiPFlash for a WPF application that has been developed in our office. In order to do this I have had to add name attributes to several of WPF components that up until now have not needed them.

I have hit a block though in trying to set unique names to a set of objects that are created at runtime. The XAML looks like:

<UserControl x:Class="Atlas.Activities.RibbonActivity.RibbonActivityView" 
                     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
                     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
                     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
                     xmlns:ribbon="clr-namespace:Microsoft.Windows.Controls.Ribbon;assembly=RibbonControlsLibrary" 
                     xmlns:RoutedMessaging="clr-namespace:Caliburn.PresentationFramework.RoutedMessaging;assembly=Caliburn.PresentationFramework" mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="300">

<ribbon:RibbonButton Style="{StaticResource RibbonButtonView}" Name="RibbonActivity">
    <ribbon:RibbonButton.ContextMenu>
        <ribbon:RibbonContextMenu>
            <ribbon:RibbonMenuItem x:Name="Modify" Header="Modify..." RoutedMessaging:Message.Attach="[Event Click] = [Action Modify]" />
            <ribbon:RibbonMenuItem x:Name="Hide" Header="Hide" RoutedMessaging:Message.Attach="[Event Click] = [Action Hide]" />
            <ribbon:RibbonMenuItem x:Name="Delete" Header="Delete" RoutedMessaging:Message.Attach="[Event Click] = [Action Delete]" />
        </ribbon:RibbonContextMenu>
    </ribbon:RibbonButton.ContextMenu>
</ribbon:RibbonButton>

The constructor set the subject using:

WithSubject(activity);

which is inhereted from Caliburn.PresentationFramework.Screens.

So what I want to do is set the name of each control to be the name field within the subject (activity).

Can anyone help me out, or at least let me know if this is possible,

Thanks in advance,

Klee.

Klee
  • 2,022
  • 3
  • 23
  • 32
  • I don't understand what the problem is? Any Windows.Controls.Control item has a .Name property. Just set .Name to be whatever you want, right? Please clarify your question? – abelenky Apr 01 '11 at 03:33
  • What i want is to be able to set the name property at the object creation so rather than __ I want to set it with a binding similar to: Or at creation time in the C# code. – Klee Apr 01 '11 at 03:41
  • And when you try setting the Name to a {Binding}, what kind of trouble do you run into? I'm under the impression that applying a data-binding to the Name property will work. – abelenky Apr 01 '11 at 04:03
  • @abelenky Binding in the way described above gives an compilation error: _MarkupExtensions are not allowed for Uid or Name property values, so '{Binding Subject.name}' is not valid._ – Klee Apr 01 '11 at 04:23
  • And I'm sure you can show us in your original question where you stated that you got a compilation error, and quoted the error text, right? – abelenky Apr 01 '11 at 06:46

1 Answers1

7

As per your example of what your trying to achieve

Style="{StaticResource RibbonButtonView}" Name="{Binding Subject.name}">

You cannot do this in XAML. XAML is compiled and the name element is used in the compilation process so that you can access the XAML element in the code-behind. When the XAML compiler converts the XML element into C# code, it uses the Name property as the name of the variable in a partial class. It isn't a property that can be set at runtime by the WPF binding system. Check out this great SO question for more details about this behaviour.

If you want to dynamically give controls names, you will need to create them in the code-behind, but I don't know if this will achieve what you're trying to do since I don't know what you're using the Name property for.

Community
  • 1
  • 1
Dave White
  • 3,451
  • 1
  • 20
  • 25