两个值在WPF中相互依赖

时间:2021-09-13 18:04:27

I want to make some two values dependent on each other, for example:

我想让两个值相互依赖,例如:

first_value = 0.6f then second_value = 0.4f [range: 0.0f to 1.0f], 
so first_value = 1.0f - second_value and second_value = 1.0f - first_value

How to do this in WPF? And how to update it in 'real time'?

如何在WPF中执行此操作?以及如何“实时”更新它?

1 个解决方案

#1


0  

Option 1 If you are using the MVVM design pattern, then you can use something like this in your ViewModel:

选项1如果您使用的是MVVM设计模式,那么您可以在ViewModel中使用以下内容:

public float FirstValue {
    get { return _firstValue; }
    set { 
        _firstValue = value;
        _secondValue = 1 - value;
        OnPropertyChanged("FirstValue");
        OnPropertyChanged("SecondValue");
    }
}

 public float SecondValue {
    get { return _secondValue; }
    set { 
        _secondValue = value;
        _firstValue = 1 - value;
        OnPropertyChanged("FirstValue");
        OnPropertyChanged("SecondValue");
    }
}

Or,

Option 2 If you want to achieve this directly is in your XAML (UI) - you can add a two-way binding and a converter (IValueConverter).

选项2如果要直接在XAML(UI)中实现此功能 - 可以添加双向绑定和转换器(IValueConverter)。

For e.g. to keep 2 sliders in sync with each other real-time:

对于例如保持2个滑块实时同步:

<Slider Name="slider1" Value="{Binding ElementName=slider2, Path=Value, Mode=TwoWay, Converter={ StaticResource converter}}" />
<Slider Name="slider2" />

and implement your converter like this:

并像这样实现你的转换器:

 public class FirstToSecondConverter : IValueConverter
 {
    public object Convert(object value, Type targetType, 
        object parameter, CultureInfo culture)
    {
         // Do the conversion here from first to second value
    }

    public object ConvertBack(object value, Type targetType, 
         object parameter, CultureInfo culture)
    {
         // Do the conversion from second to first value
    }
}

#1


0  

Option 1 If you are using the MVVM design pattern, then you can use something like this in your ViewModel:

选项1如果您使用的是MVVM设计模式,那么您可以在ViewModel中使用以下内容:

public float FirstValue {
    get { return _firstValue; }
    set { 
        _firstValue = value;
        _secondValue = 1 - value;
        OnPropertyChanged("FirstValue");
        OnPropertyChanged("SecondValue");
    }
}

 public float SecondValue {
    get { return _secondValue; }
    set { 
        _secondValue = value;
        _firstValue = 1 - value;
        OnPropertyChanged("FirstValue");
        OnPropertyChanged("SecondValue");
    }
}

Or,

Option 2 If you want to achieve this directly is in your XAML (UI) - you can add a two-way binding and a converter (IValueConverter).

选项2如果要直接在XAML(UI)中实现此功能 - 可以添加双向绑定和转换器(IValueConverter)。

For e.g. to keep 2 sliders in sync with each other real-time:

对于例如保持2个滑块实时同步:

<Slider Name="slider1" Value="{Binding ElementName=slider2, Path=Value, Mode=TwoWay, Converter={ StaticResource converter}}" />
<Slider Name="slider2" />

and implement your converter like this:

并像这样实现你的转换器:

 public class FirstToSecondConverter : IValueConverter
 {
    public object Convert(object value, Type targetType, 
        object parameter, CultureInfo culture)
    {
         // Do the conversion here from first to second value
    }

    public object ConvertBack(object value, Type targetType, 
         object parameter, CultureInfo culture)
    {
         // Do the conversion from second to first value
    }
}