c# – 将ValueConverter存储到变量

c# – 将ValueConverter存储到变量,第1张

概述我有一个ValueConverter用于在StoryBoard动画中绑定’To’Value,类似于答案- WPF animation: binding to the “To” attribute of storyboard animation. 问题是我在几个地方重复MultiBinding ValueConverter的下面一段代码. <MultiBinding Converter="{Stat 我有一个ValueConverter用于在StoryBoard动画中绑定’To’Value,类似于答案- WPF animation: binding to the “To” attribute of storyboard animation.

问题是我在几个地方重复MultiBinding ValueConverter的下面一段代码.

<MultiBinding Converter="{StaticResource multiplyConverter}">       <Binding Path="ActualHeight" Elementname="ExpanderContent"/>       <Binding Path="Tag" relativeSource="{relativeSource Self}" />    </MultiBinding>

我想通过将ValueConverter的结果存储到资源变量来删除此重复代码,以便我可以将此本地变量直接绑定到故事板.

<system:Double x:Key="CalculateDWIDth">    <MultiBinding Converter="{StaticResource multiplyConverter}">        <Binding Path="ActualHeight" Elementname="ExpanderContent"/>        <Binding Path="Tag" relativeSource="{relativeSource Self}" />    </MultiBinding></system:Double >

我收到以下错误:

The type ‘Double’ does not support direct content.

Cannot add content to an object of type “Double”.

我觉得这是一个常见问题,但无法找到解决方案来消除这种冗余.

更新

谢谢Rohit,你的答案解决了这个问题.但我还有一个相关的问题,所以更新问题.此变量CalculateDWIDth在正常情况下工作正常,但在Rendertransform中使用它时,它不会获取该值.即如果我使用正常的方式使用转换器它可以工作,但它不会获取变量.

<StackPanel.Rendertransform>    <Translatetransform x:name="SlIDertransform">        <Translatetransform.X>            <Binding Converter="{StaticResource PanelConverter}" Elementname="SlIDerPanel" Path="ActualWIDth" /> // Works            <Binding Path="WIDth" Source="{StaticResource CalculateDWIDth}"/> // Doesn't Work        </Translatetransform.X>    </Translatetransform></StackPanel.Rendertransform>

我将变量保留为本地资源的一部分.这是否意味着在调用Render变换时不会创建变量?

解决方法 由于错误提示您无法与Double绑定.绑定只能使用Dependency属性完成.

而是在资源中使用FrameworkElement并绑定其宽度(DP),如下所示:

<FrameworkElement x:Key="CalculateDWIDth">    <FrameworkElement.WIDth>        <MultiBinding Converter="{StaticResource multiplyConverter}">            <Binding Path="ActualHeight" Elementname="ExpanderContent"/>            <Binding Path="Tag" relativeSource="{relativeSource Self}" />        </MultiBinding>     </FrameworkElement.WIDth></FrameworkElement>

你可以像这个样本一样绑定这个资源:

<TextBlock WIDth="{Binding WIDth,Source={StaticResource CalculateDWIDth}}"/>
总结

以上是内存溢出为你收集整理的c# – 将ValueConverter存储到变量全部内容,希望文章能够帮你解决c# – 将ValueConverter存储到变量所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/1251773.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-07
下一篇2022-06-07

发表评论

登录后才能评论

评论列表(0条)

    保存