gpt4 book ai didi

c# - MultiValueConverter 无法绑定(bind)到 ViewModel

转载 作者:太空宇宙 更新时间:2023-11-03 21:13:25 24 4
gpt4 key购买 nike

HelloAll:我正在尝试创建一个用户控件,它需要一个 MultiValueConverter 来在我的应用程序中缩放 Canvas :

它需要

  1. Canvas.ActualWidth
  2. X 最小工程单位
  3. X 最大工程单位

.

public class MultiValueScaleTransform : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
double numerator = (double)values[0];
double denominator = (double)values[2] - (double)values[1];
return numerator / denominator;
}

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

当我给它输入数字时这似乎工作正常但是当我将我的 XAML 绑定(bind)到 View 模型时出现问题:

这是错误消息:

Error 7 A 'Binding' cannot be set on the 'Path' property of type 'Binding'. A 'Binding' can only be set on a DependencyProperty of a DependencyObject. C:\Users\mwardell\Documents\Visual Studio 2013\Projects\HalliburtonCallouts (12)\HalliburtonCallouts\HalliburtonCallouts\View\UserControls\WellViewUserCtrl.xaml 31 38 HalliburtonCallouts

 <UserControl x:Class="HalliburtonCallouts.View.UserControls.WellViewUserCtrl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:Converters="clr-namespace:HalliburtonCallouts.ViewModel.Converters"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300"
x:Name="uc">
<UserControl.Resources>
<Converters:ColorToImageBrush x:Key="ColorToBrush"/>
<Converters:ColorToBitmapBrush x:Key="ColorToImg"/>
<Converters:ColorToBG x:Key="ColorToBG"/>
<Converters:ColorToFG x:Key="ColorToFG"/>
<Converters:MultiValueScaleTransform x:Key="ScaleTransform"/>
<SolidColorBrush x:Key="BlueBg" Color="#FFA9DCF1"/>
</UserControl.Resources>
<Border Background="Red">
<StackPanel>
<!-- I used these to make sure the bindings of the user control are working-->
<TextBlock Text="OverallStartDepth"></TextBlock>
<TextBlock Text="{Binding OverallStartDepth}"></TextBlock>
<TextBlock Text="OverallEndDepth"></TextBlock>
<TextBlock Text="{Binding OverallEndDepth}"></TextBlock>
<Canvas x:Name="WellCanvas"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
DataContext="{Binding Mode=OneWay, RelativeSource={RelativeSource PreviousData}}" >
<Canvas.RenderTransform>
<ScaleTransform >
<ScaleTransform.ScaleX >
<MultiBinding Converter="{StaticResource ScaleTransform}">
<Binding Path="ActualWidth"/>
<Binding Path="{Binding OverallStartDepth, FallbackValue=0.0}"/>
<Binding Path="{Binding OverallEndDepth,FallbackValue=100.0}"/>
</MultiBinding>
</ScaleTransform.ScaleX>

</ScaleTransform>
</Canvas.RenderTransform>
</Canvas>
</StackPanel>
</Border>
</UserControl>

我已经确定 OverallEndDepthOverallStartDepth 是可绑定(bind)的。请参阅 StackPanel 前四项左右。可绑定(bind)性不能证明它们是 Dep 对象的 Dep 属性吗?

最佳答案

您不能也不会在 MultiBinding 中绑定(bind)绑定(bind)的 Path 属性。相反,您只需将它们设置为

<TextBlock Text="{Binding OverallStartDepth}">

相当于

<TextBlock Text="{Binding Path=OverallStartDepth}">

还有

<TextBlock>
<TextBlock.Text>
<Binding Path="OverallStartDepth"/>
</TextBlock.Text>
</TextBlock>

所以 MultiBinding 应该这样写:

<Canvas ... >
<Canvas.RenderTransform>

<MultiBinding Converter="{StaticResource ScaleTransform}">
<Binding Path="ActualWidth" RelativeSource="{RelativeSource AncestorType=Canvas}"/>
<Binding Path="OverallStartDepth" FallbackValue="0.0"/>
<Binding Path="OverallEndDepth" FallbackValue="100.0"/>
</MultiBinding>
</Canvas.RenderTransform>
</Canvas>

还要确保你删除

DataContext="{Binding Mode=OneWay, RelativeSource={RelativeSource PreviousData}}"

来自 Canvas

关于c# - MultiValueConverter 无法绑定(bind)到 ViewModel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36122965/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com