2016-02-04 83 views
0

我想知道如果在XAML中未觸摸視圖模型,我可以執行類似thisthis的操作,但使用其他屬性的比例。WPF將屬性的值設置爲另一個屬性的值的比率

我有內部2個橢圓一個按鈕控制和我想要的橢圓中的一個的餘量,這取決於其它的高度而變化。

因此,像:

<Ellipse Margin=.2*"{Binding ElementName=OtherEllipse, Path=Height}"/> 

回答

0

MainWindow.xaml

<Window x:Class="MultiBindingConverterDemo.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    xmlns:local="clr-namespace:MultiBindingConverterDemo" 
    mc:Ignorable="d" 
    Title="MainWindow" Height="600" Width="800"> 
<StackPanel> 
    <StackPanel.Resources> 
     <local:MultiplyValueConverter x:Key="MultiplyValueConverter"/> 
    </StackPanel.Resources> 
    <Ellipse x:Name="OtherEllipse" Width="100" Height="50" Fill="Red"/> 
    <Ellipse Width="50" Height="50" Fill="Blue" 
      Margin="{Binding Path=Height, 
           ElementName=OtherEllipse, 
           Converter={StaticResource MultiplyValueConverter}, 
           ConverterParameter=0.2}"> 
    </Ellipse> 
</StackPanel> 

MainWindow.xaml.cs

using System; 
using System.Globalization; 
using System.Windows; 
using System.Windows.Data; 

namespace MultiBindingConverterDemo 
{ 
    public partial class MainWindow : Window 
    { 
     public MainWindow() 
     { 
      InitializeComponent(); 
     } 
    } 

    public class MultiplyValueConverter : IValueConverter 
    { 
     public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 
     { 
      double height = (double)value; 
      double multiplier = double.Parse((string)parameter); 
      return new Thickness(height * multiplier); 
     } 

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

謝謝!!我似乎仍然無法完成它的工作,可能與它處於一種風格有關,但我正在努力! – azulBonnet

相關問題