2016-09-16 161 views
1

我需要在wpf .xaml中總結兩個綁定(數字),而不需要使用後端c#代碼。這是可行的,以及如何?Xaml。如何總結兩個綁定,而不需要c#代碼

我有兩個文本塊,我需要總結到第三個。

<TextBlock x:Name="ChosenAmountValue" Grid.Row="0" Grid.Column="2" 
Style="{DynamicResource BalanceDisplayDetailText}" Text="{Binding _Transfer.TransferAmount , StringFormat={}{0}.00}" IsEnabled="False" TextAlignment="Center" Background="Transparent"/> 

    <TextBlock x:Name="SurchargeFeesValue" Grid.Row="2" Grid.Column="2" Style="{DynamicResource BalanceDisplayDetailText}" Text="{Binding _Transfer.Fee.Value}" TextAlignment="Center" Background="Transparent"/> 

    <TextBlock x:Name="SUM" Grid.Row="3" Grid.Column="3" Style="{DynamicResource BalanceDisplayDetailText}"Text="{???}" TextAlignment="Center" Background="Transparent"/> 
+0

你有什麼訪問?因爲最簡單的方法是爲此擁有一個單獨的屬性,第二個最簡單的方法是有一個轉換器(自定義或[第三方](http://stackoverflow.com/questions/3572432/can-i-add-subtract-值 - 即 - 被結合到一個元素屬性))。 XAML不是最適合邏輯的地方,它不打算做這樣的操作(儘管可能有一種方法)。 – icebat

回答

-1
<UserControl.Resources> 
    <converters:ValuesAdditionConverter x:Key="ValuesAddition" /> 
</UserControl.Resources> 

<TextBlock x:Name="ChosenAmountValue" Grid.Row="0" Grid.Column="2" Style="{DynamicResource BalanceDisplayDetailText}" Text="{Binding _Transfer.TransferAmount , StringFormat={}{0}.00}" IsEnabled="False" TextAlignment="Center" Background="Transparent"/> 
<TextBlock x:Name="SurchargeFeesValue" Grid.Row="2" Grid.Column="2" Style="{DynamicResource BalanceDisplayDetailText}" Text="{Binding _Transfer.Fee.Value}" TextAlignment="Center" Background="Transparent"/> 
<TextBlock x:Name="SUM" Grid.Row="3" Grid.Column="3" Style="{DynamicResource BalanceDisplayDetailText}" 
    TextAlignment="Center" Background="Transparent"> 
      <TextBlock.Text> 
       <MultiBinding Converter="{StaticResource ValuesAddition}" > 
        <Binding Path="_Transfer.TransferAmount"/> 
        <Binding Path=" _Transfer.Fee.Value" /> 
       </MultiBinding> 
      </TextBlock.Text> 
</TextBlock> 

and use a converter like this. 

class ValuesAdditionConverter : IMultiValueConverter 
{ 
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture) 
    { 
     return (int)values[0] + (int)values[1]; 
    } 

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture) 
    { 
     throw new NotImplementedException(); 
    } 
} 
+0

你知道有一個轉換器實際上是C#代碼,所以它打敗了答案的目的? – Noctis

+0

很抱歉,不幸的是我不能使用C#代碼,我試圖從現有的轉換器或任何其他方法 –