首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >MultiValueConverter未能绑定到ViewModel

MultiValueConverter未能绑定到ViewModel
EN

Stack Overflow用户
提问于 2016-03-21 04:06:17
回答 2查看 1.2K关注 0票数 1

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

它需要

  1. Canvas.ActualWidth
  2. 工程单位X Min
  3. X-最大英单位

代码语言:javascript
运行
复制
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不能在“绑定”类型的“路径”属性上设置“绑定”。“绑定”只能设置在DependencyProperty的DependencyObject上。C:\User\mwardell\Documents\Visual 2013\Projects\HalliburtonCallout (12)\HalliburtonCallouts\HalliburtonCallouts\View\UserControls\WellViewUserCtrl.xaml 31 38 HalliburtonCallouts

代码语言:javascript
运行
复制
 <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是Bindable的。请参阅StackPanel前四项左右。Bindability不证明它们是Dep对象的Dep属性吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-03-21 08:06:37

您不能也不会绑定MultiBinding中绑定的MultiBinding属性。相反,你只是把他们放进去

代码语言:javascript
运行
复制
<TextBlock Text="{Binding OverallStartDepth}">

这相当于

代码语言:javascript
运行
复制
<TextBlock Text="{Binding Path=OverallStartDepth}">

并且还可以

代码语言:javascript
运行
复制
<TextBlock>
    <TextBlock.Text>
        <Binding Path="OverallStartDepth"/>
    </TextBlock.Text>
</TextBlock>

所以MultiBinding应该是这样写的:

代码语言:javascript
运行
复制
<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>

也要确保你移除

代码语言:javascript
运行
复制
DataContext="{Binding Mode=OneWay, RelativeSource={RelativeSource PreviousData}}"

从画布上

票数 2
EN

Stack Overflow用户

发布于 2016-03-21 04:27:47

正如错误所述,您不能使用{Binding},而是可以按其名称获取元素,并从其text属性中获取值。

代码语言:javascript
运行
复制
<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是一个依赖属性- 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

//源路径(用于CLR绑定)。公共PropertyPath路径{ get {返回_ppath;} set { CheckSealed();_ppath =值;_attachedPropertiesInPath = -1;if (_ppath != null & _ppath.StartsWithStaticProperty) { if (_sourceInUse == SourceProperties.None _sourceInUse == SourceProperties.StaticSource ==SourceProperties.StaticSource) // (用于compat - Dev11 738992) { SourceReference = StaticSourceRef;}否则抛出新的InvalidOperationException(SR.Get(SRID.BindingConflict,SourceProperties.StaticSource,_sourceInUse));> }

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36122965

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档