1

I'm a complete .NET/C# nublet. I've inherited some code that defines the UI for a window with XAML and I'm trying to interface with some existing XML garbage. This is probably a very simple question; I just don't know what to Google.

I have a TextBox that I want to disable based on a boolean. I can do that with this code:

Listing A:

<TextBox x:Name="ServerNameTextBox" ... IsEnabled="{Binding ServerName.Editable}" />

The problem comes when I want to base it on a variable and a checkbox. I've already read about IMultiValueConverter and wrote one for this, but I'm not sure how to specify the bindings because the format changes.

Listing B:

<TextBox x:Name="ServerNameTextBox" ... >
    <TextBox.IsEnabled>
        <Binding ElementName="CheckBoxServerNameEnabled" Path="IsChecked" />
        <Binding ??? />
     </TextBox.IsEnabled>
</TextBox>

In order to make the same reference that's made in the first line, what needs to go in the question marks? Bonus points for telling me what the type of binding using in Listing A is called.

Thanks!

H.B.
  • 166,899
  • 29
  • 327
  • 400
Jay
  • 510
  • 6
  • 17

3 Answers3

3

{Binding ServerName.Editable} is (in this case) equivalent to {Binding Path=ServerName.Editable}

So in your MultiBinding you have

<MultiBinding Converter="...">
    <Binding ElementName="CheckBoxServerNameEnabled" Path="IsChecked"/>
    <Binding Path="ServerName.Editable"/>
</MultiBinding>

In markup extensions the unnamed arguments are passed to the constructor, Binding has a constructor which takes a path.

Community
  • 1
  • 1
H.B.
  • 166,899
  • 29
  • 327
  • 400
  • How are parameters passed into the markup extension constructor? Or, alternatively, how are markup extension arguments set? – ergohack Apr 04 '17 at 18:31
  • @JRo: It's all reflection under the hood, what exactly do you want to know and why? I.e. what is your goal/problem? – H.B. Apr 06 '17 at 07:00
  • From your example I couldn't see how to pass parameters to the markup extension as all of the attributes apply to the `Binding` base class. I (inexpertly) played around with the `` element and sub-elements and posted an answer below with what I found. In short, the markup extension class can be used as a `` element (e.g. ``). Then the parameters for the extension are just attributes on the XML element. – ergohack Apr 06 '17 at 18:47
  • 1
    @JRo: Well, as markup extensions can appear in element form i always make sure that they can be used with property assignments only and all non-empty constructors are optional. – H.B. Apr 07 '17 at 05:59
  • That makes a lot of sense, especially in this context. Thanks for the perspective. – ergohack Apr 07 '17 at 17:16
1

You're almost there. Check http://msdn.microsoft.com/en-us/library/system.windows.data.multibinding.aspx

franssu
  • 2,422
  • 1
  • 20
  • 29
0

Since the markup extension class is derived from Binding, I was able to substitute the XML element tags. E.g.:

Before:

   <local:FolderBox Text="{xset:XSettings Folders.BuildRoot}"/>

After:

   <local:FolderBox>
      <local:FolderBox.Text>
         <MultiBinding Converter="{StaticResource Precedence}" Mode="TwoWay">
            <xset:XSettings Prefix="Folders.BuildRoot" BindNow="True"/>
            <Binding ElementName="BuildRoot" Path="Text"/>
         </MultiBinding>
      </local:FolderBox.Text>
   </local:FolderBox>

But, the constructor is now called without parameters. So I had to use "ConstructorArgument"s, with the last, "BindNow" argument used to logically run what would have been the constructor code. E.g.:

public class XSettingsExtension : Binding
{
   [ConstructorArgument("Prefix")]
   public string Prefix { get; set; }

   private bool _BindingSet;
   [ConstructorArgument("BindNow")]
   public bool BindNow
   {
      get { return this._BindingSet; }
      set { this._BindingSet = value; SetBinding(); }
   }
ergohack
  • 1,268
  • 15
  • 27