簡體   English   中英

StringFormat XAML綁定到多個控件

[英]StringFormat XAML Binding to multiple controls

我有一個自定義的Button類型,我無法更改代碼。 此Button具有一個名為MyArguments的屬性,該屬性接受一串用分號分隔的值。

屏幕上有一堆TextBoxes供用戶輸入一些信息。

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

我希望我的Button接受這三個值並將它們提供給MyArguments字符串屬性。

如果只有一個TextBox,我可以使用StringFormat選項,如下所示:

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

但是,不能將多個控件與StringFormat一起使用。

我嘗試使用MultiBinding,但是MyArguments屬性給出錯誤'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>

我需要在純XAML中完成此操作。 后面沒有代碼。

有任何想法嗎?

您忘記將名稱空間前綴(例如local )添加到屬性:

<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>

聽起來解析器無法將元素語法屬性與元素實例相關聯。 即,它認為您正在定義附加屬性,即使它應該是實例屬性也是如此。

例如

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

您引用的是同一類型嗎? 您的按鈕上是否沒有前綴也很奇怪,還是有人實際上濫用了系統並編譯了程序集以使用WPF名稱空間?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM