簡體   English   中英

MultiValueConverter無法綁定到ViewModel

[英]MultiValueConverter Failing to Bind to ViewModel

HelloAll:我正在嘗試創建一個需要MultiValueConverter來縮放App中畫布的用戶控件:

它需要

  1. 畫布實際寬度
  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綁定到視圖模型時,就會出現問題:

這是錯誤消息:

錯誤7無法在類型“綁定”的“路徑”屬性上設置“綁定”。 只能在DependencyObject的DependencyProperty上設置“綁定”。 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是可綁定的。 請參閱StackPanel的前四個項目。 可綁定性是否不能證明它們是Dep對象的Dep屬性?

如錯誤所述,您不能使用{Binding} ,而是可以按元素名稱獲取元素,並從其text屬性獲取值。

<TextBlock Text="OverallStartDepth"></TextBlock>
<TextBlock Name="OverallStartDepthTextBlock" Text="{Binding OverallStartDepth}"></TextBlock>
<TextBlock Text="OverallEndDepth"></TextBlock>
<TextBlock Name="OverallEndDepthTextBlock" Text="{Binding OverallEndDepth}"></TextBlock>
<MultiBinding Converter="{StaticResource ScaleTransform}">
    <Binding Path="ActualWidth"/>
    <Binding ElementName="OverallStartDepthTextBlock" Path="Text"/>
    <Binding ElementName="OverallEndDepthTextBlock" Path="Text"/>
</MultiBinding>

注意:您可以綁定OverallEndDepth是可綁定到任何控件的依賴項屬性的值。

例如TextBlock.Text是Dependency屬性-SourceCode

  /// <summary> /// DependencyProperty for <see cref="Text" /> property. /// </summary> [CommonDependencyProperty] public static readonly DependencyProperty TextProperty = DependencyProperty.Register( "Text", typeof(string), typeof(TextBlock), new FrameworkPropertyMetadata( string.Empty, FrameworkPropertyMetadataOptions.AffectsMeasure | FrameworkPropertyMetadataOptions.AffectsRender, new PropertyChangedCallback(OnTextChanged), new CoerceValueCallback(CoerceText))); /// <summary> /// The Text property defines the content (text) to be displayed. /// </summary> [Localizability(LocalizationCategory.Text)] public string Text { get { return (string) GetValue(TextProperty); } set { SetValue(TextProperty, value); } } 

但是,Binding.Path是一個普通屬性,無法綁定,因此會出現上述錯誤-SourceCode

 /// <summary> The source path (for CLR bindings).</summary> public PropertyPath Path { get { return _ppath; } set { CheckSealed(); _ppath = value; _attachedPropertiesInPath = -1; ClearFlag(BindingFlags.PathGeneratedInternally); if (_ppath != null && _ppath.StartsWithStaticProperty) { if (_sourceInUse == SourceProperties.None || _sourceInUse == SourceProperties.StaticSource || FrameworkCompatibilityPreferences.TargetsDesktop_V4_0) // (for compat - Dev11 738992) { SourceReference = StaticSourceRef; } else throw new InvalidOperationException(SR.Get(SRID.BindingConflict, SourceProperties.StaticSource, _sourceInUse));> } } } 

您不能也不會在MultiBinding中綁定Bindings的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}}"

來自畫布

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM