WPF MultiBinding Ellipse Fill

时间:2018-01-22 14:46:55

标签: wpf xaml fill ellipse imultivalueconverter

我无法让MultiBinding Ellipse.Fill正常工作。我确实(单个)绑定工作正常,加上MultiBinding上的Ellipse.Tooltip

<Ellipse Margin="210,56,0,0" Fill="{Binding InspectorPC, Converter={StaticResource statusButtonConverter}, Mode=OneWay}">
    <Ellipse.ToolTip>
        <MultiBinding Converter="{StaticResource statusStringConverter}" Mode="OneWay">
            <Binding Path="InspectorPC"/>
            <Binding Path="InspectorPCPing"/>
            <Binding Path="InspectorPCReadHD"/>
        </MultiBinding>
    </Ellipse.ToolTip>
</Ellipse>

但我想要像:

<Ellipse Margin="210,56,0,0">
    <Ellipse.Fill>
        <MultiBinding Converter="{StaticResource statusButtonConverter}" Mode="OneWay">
            <Binding Path="InspectorPCPing"/>
            <Binding Path="InspectorPCReadHD"/>
        </MultiBinding>
    </Ellipse.Fill>
    <Ellipse.ToolTip>
        <MultiBinding Converter="{StaticResource statusStringConverter}" Mode="OneWay">
            <Binding Path="InspectorPC"/>
            <Binding Path="InspectorPCPing"/>
            <Binding Path="InspectorPCReadHD"/>
        </MultiBinding>
    </Ellipse.ToolTip>
</Ellipse>

(显然statusButtonConverter需要从IValueConverter更改为IMultiValueConverter,但这不是问题。)

1 个答案:

答案 0 :(得分:2)

如果这不起作用,则表明您的statusButtonConverter实施中存在问题。

一个简单的示例显示将MultiBinding应用于Ellipse.Fill

没有问题
<Window x:Class="WpfTest.FillMultiBinding"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:l="clr-namespace:WpfTest2"
        Width="320"
        Height="160">
  <Window.Resources>
    <l:BrushPartsConverter x:Key="brushPartsConverter" />
  </Window.Resources>
  <Window.DataContext>
    <l:FillViewModel />
  </Window.DataContext>
  <Ellipse>
    <Ellipse.Fill>
      <!-- Dodger + Blue = DodgerBlue -->
      <MultiBinding Converter="{StaticResource brushPartsConverter}" Mode="OneWay">
        <Binding Path="Part1" />
        <Binding Path="Part2" />
      </MultiBinding>
    </Ellipse.Fill>
  </Ellipse>
</Window>
public class FillViewModel
{
    public string Part1 => "Dodger";
    public string Part2 => "Blue";
}

public class BrushPartsConverter : IMultiValueConverter
{
    private static readonly BrushConverter InnerConverter = new BrushConverter();

    public object Convert(object[] values, Type type, object p, CultureInfo c)
    {
        if (values?.Length == 2)
            return InnerConverter.ConvertFrom("" + values[0] + values[1]);
        return DependencyProperty.UnsetValue;
    }

    public object[] ConvertBack(object value, Type[] types, object p, CultureInfo c)
    {
        return new[] { DependencyProperty.UnsetValue };
    }
}

Screenshot

发布转换器和绑定上下文的代码(视图模型),我们将看到我们可以做什么。