是否有一个 IValueConverter 可以执行 if-then

Jon*_*nny 2 c# wpf xaml

我正在尝试做的事情:

<Grid>
  <Grid.RowDefinitions>
    ...
    <!--The next line is pseudo code for what I am trying to achieve-->
    <RowDefintion Height="if(EditEnabled) { 10* } else { 0 }" />
    ...
  </Grid.RowDefinition>
  ...
  <DockPanel Visibility="{Binding EditEnabled, Converter={StaticResource InverseBooleanToVisibilityConverter}}" ...>
  ...
Run Code Online (Sandbox Code Playgroud)

我试图根据是否启用编辑来更改 DockPanel 的可见性,同时保持调整大小并具有固定高度和相对高度的能力。

问题:

是否有一个IValueConverter( System.Windows.Data.IValueConverter) 可以接受一个布尔值和两个数字,并GridLength根据布尔值选择其中一个?仅检查接口IValueConverter,这似乎不是正确使用的类型。

或者有更好的方法来注入GridLength我想要的吗?

我尝试过的:

  • 翻阅继承者IValueConverter- 对我来说没有什么明显的
  • 移动标签Height="10*"内部DockPanel并更改RowDefinitionAuto- 这会产生转换异常
  • 在这里搜索

Hak*_*tık 5

不幸的是,没有一个IValueConverter可以做到“if-then”。
(更具体地说:您无法使用 XAML 执行 if-then 逻辑)
但您可以在 C# 代码中执行 if-then 逻辑。
这是解决方案

public class HeightConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        bool enableEdit = (bool)value;
        double param = System.Convert.ToDouble(parameter);

        if (enableEdit)
            return new GridLength(param, GridUnitType.Star);
        else
            return new GridLength(0);
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}
Run Code Online (Sandbox Code Playgroud)

和这样的窗户。

<Window.Resources>
    <local:HeightConverter x:Key="heightConverter"/>
    <sys:Int32 x:Key="param">10</sys:Int32>
</Window.Resources>
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="{Binding Path=EditEnabled, Converter={StaticResource heightConverter}, ConverterParameter={StaticResource param}}" />
    </Grid.RowDefinitions>
</Grid>
Run Code Online (Sandbox Code Playgroud)

请考虑还定义您将使用的所需命名空间,如下所示

xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:local="clr-namespace:[your namespace]"
Run Code Online (Sandbox Code Playgroud)

使用IMutliValueConverter可以实现更新相同的结果

public class HeightMultiConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        bool enableEdit = (bool)values[0];
        double param = System.Convert.ToDouble(values[1]);

        if (enableEdit)
            return new GridLength(param, GridUnitType.Star);
        else
            return new GridLength(0);
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}
Run Code Online (Sandbox Code Playgroud)

和这样的窗户

<Window.Resources>
    <local:HeightMultiConverter x:Key="heightMutliConverter"/>
    <sys:Int32 x:Key="param">10</sys:Int32>
</Window.Resources>
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition >
            <RowDefinition.Height>
                <MultiBinding Converter="{StaticResource heightMutliConverter}">
                    <Binding Path="EditEnabled"/>
                    <Binding Source="{StaticResource param}"/>
                </MultiBinding>
            </RowDefinition.Height>
        </RowDefinition>
    </Grid.RowDefinitions>
</Grid>
Run Code Online (Sandbox Code Playgroud)

Source注意:请不要忘记,您必须通过设置属性来处理DataContext