Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WPF将一个属性的值设置为另一个属性值的比率_C#_Wpf_Xaml_Binding - Fatal编程技术网

C# WPF将一个属性的值设置为另一个属性值的比率

C# WPF将一个属性的值设置为另一个属性值的比率,c#,wpf,xaml,binding,C#,Wpf,Xaml,Binding,我想知道在XAML中,在不涉及视图模型的情况下,我是否可以使用其他属性的比率 我有一个按钮控件,里面有两个椭圆,我希望其中一个椭圆的边距随另一个椭圆的高度而变化 比如: <Ellipse Margin=.2*"{Binding ElementName=OtherEllipse, Path=Height}"/> 您可以,您需要编写自定义的IValueConverter 如果需要传递参数:MainWindow.xaml <Window x:Class="MultiBindingC

我想知道在XAML中,在不涉及视图模型的情况下,我是否可以使用其他属性的比率

我有一个按钮控件,里面有两个椭圆,我希望其中一个椭圆的边距随另一个椭圆的高度而变化

比如:

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

您可以,您需要编写自定义的IValueConverter

如果需要传递参数:

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>

非常感谢。我似乎仍然无法让它工作,可能与它的风格有关,但我正在努力!
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();
        }
    }
}