简体   繁体   中英

StringFormat XAML Binding to multiple controls

I have a custom Button type and i cannot change the code. This Button has an property called MyArguments which accepts a string of semicolon separated values.

I have a bunch of TextBoxes on the screen for the user to enter some information.

<TextBox Name="TestTextBox1" />
<TextBox Name="TestTextBox2" />
<TextBox Name="TestTextBox3" />

I want my Button to take these three values and supply them to the MyArguments string property.

If there was only a single TextBox i could use the StringFormat option like this:

<MyButton MyArguments="{Binding ElementName=TestTextBox1, Path=Text, StringFormat='Arguments;{0}' }/>

However you cannot use multiple controls with StringFormat.

I tried using MultiBinding but the MyArguments property gives an error 'The attachable property 'MyArguments' was not found in type MyButton' .

<MyButton.MyArguments>
    <MultiBinding StringFormat="Arguments;{0};{1}">
        <Binding ElementName="TestTextBox1" Path="Text" />
        <Binding ElementName="TestTextBox2" Path="Text" />
    </MultiBinding>
</MyButton.MyArguments>

I need this done in pure XAML. No code behind.

Any ideas?

You forgot to add the namespace prefix (eg local ) to the property:

<local:MyButton>
    <local:MyButton.MyArguments>
        <MultiBinding StringFormat="Arguments;{0};{1}">
            <Binding ElementName="TestTextBox1" Path="Text" />
            <Binding ElementName="TestTextBox2" Path="Text" />
        </MultiBinding>
    </local:MyButton.MyArguments>
</local:MyButton>

This sounds like the parser cannot associate the element syntax property with your element instance. Ie it thinks you are defining an attached property even though it is supposed to be an instance property.

eg

<ListBox ItemsSource="{Binding Data}">
  <ListView.ItemTemplate>
     <!-- Template -->
  </ListView.ItemTemplate>
<ListBox>

Are you referencing the same type? It also looks odd how you have no prefix on your button, or did someone actually abuse the system and compile the assembly to use the WPF namespace?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM