DataContext 作为资源内转换器绑定的源

发布于 2024-09-04 08:30:46 字数 1254 浏览 10 评论 0原文

 <Canvas.DataContext>
  <ViewModels:VMSomeControl Model="{Binding RelativeSource={RelativeSource TemplatedParent}}" />
 </Canvas.DataContext>

 <!-- DataContext is not passed into these Instances.
      they also have no knowledge of their TemplatedParent. -->
 <Canvas.Resources>

  <!--  is there a way to use a binding that points to the datacontext within the resources ? -->
  <Converters:SomeConverter x:Key="someConverter" 
                            SomeProperty="{Binding Path=Model.SomeProperty}" />

  <!--  is there a way to point Directly to the TemplatedParent  ? -->
  <Converters:SomeConverter x:Key="someConverter" 
                            SomeProperty="{TemplateBinding SomeProperty}" />

 </Canvas.Resources>


 <SomeFrameworkElement SomeProperty="{Binding Path=Model.SomeOtherProperty, Converter={StaticResource someConverter}, ConverterParameter=0}" />

 <SomeFrameworkElement SomeProperty="{Binding Path=Model.SomeOtherProperty, Converter={StaticResource someConverter}, ConverterParameter=1}" />

</Canvas>

是否可以使用使用 dataContext 或 TemplatedParent 的绑定 在 ControlTemplate 的根视觉资源中?

 <Canvas.DataContext>
  <ViewModels:VMSomeControl Model="{Binding RelativeSource={RelativeSource TemplatedParent}}" />
 </Canvas.DataContext>

 <!-- DataContext is not passed into these Instances.
      they also have no knowledge of their TemplatedParent. -->
 <Canvas.Resources>

  <!--  is there a way to use a binding that points to the datacontext within the resources ? -->
  <Converters:SomeConverter x:Key="someConverter" 
                            SomeProperty="{Binding Path=Model.SomeProperty}" />

  <!--  is there a way to point Directly to the TemplatedParent  ? -->
  <Converters:SomeConverter x:Key="someConverter" 
                            SomeProperty="{TemplateBinding SomeProperty}" />

 </Canvas.Resources>


 <SomeFrameworkElement SomeProperty="{Binding Path=Model.SomeOtherProperty, Converter={StaticResource someConverter}, ConverterParameter=0}" />

 <SomeFrameworkElement SomeProperty="{Binding Path=Model.SomeOtherProperty, Converter={StaticResource someConverter}, ConverterParameter=1}" />

</Canvas>

is it possible to use bindings that use either the dataContext or the TemplatedParent
Within a ControlTemplate's Root Visuals resourecs ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

国产ˉ祖宗 2024-09-11 08:30:46

之前的答案非常接近。但多重绑定内的绑定应该是:

<SomeFrameworkElement>
    <SomeFrameworkElement.SomeProperty>
        <MultiBinding Converter="{StaticResource someConverter}" >
            <Binding />
        </MultiBinding>        
    </SomeFrameworkElement.SomeProperty>
</SomeFrameworkElement>

对我有用

Previous answer is reeeeally really close. but the binding inside the multibinding should be :

<SomeFrameworkElement>
    <SomeFrameworkElement.SomeProperty>
        <MultiBinding Converter="{StaticResource someConverter}" >
            <Binding />
        </MultiBinding>        
    </SomeFrameworkElement.SomeProperty>
</SomeFrameworkElement>

that worked for me

云淡风轻 2024-09-11 08:30:46

如果您希望值转换器能够访问数据上下文,您可能需要使用 ConverterParameter:

<SomeFrameworkElement SomeProperty="{Binding Path=Model.SomeOtherProperty, Converter={StaticResource someConverter}, ConverterParameter={Binding DataContext}}" />

然后,数据上下文将作为参数传递到您的值转换器,以实现 < a href="http://msdn.microsoft.com/en-us/library/system.windows.data.ivalueconverter.convert(v=VS.100).aspx" rel="noreferrer">IValueConverter.Convert.


如果您实现 IMultiValueConverter 并使用 MultiBinding 类:

<SomeFrameworkElement>
    <SomeFrameworkElement.SomeProperty>
        <MultiBinding Converter="{StaticResource someConverter}" >
            <Binding Path="DataContext"/>
        </MultiBinding>        
    </SomeFrameworkElement.SomeProperty>
</SomeFrameworkElement>

元素的绑定元素传递给 IMultiValueConverter 的 Convert 方法 作为values 参数。 Convert 具有以下签名:

public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture);

If you want your value converter to be able to access the datacontext you may want to use ConverterParameter instead:

<SomeFrameworkElement SomeProperty="{Binding Path=Model.SomeOtherProperty, Converter={StaticResource someConverter}, ConverterParameter={Binding DataContext}}" />

The datacontext will then be passed on to your value converter as a parameter to your implementation of IValueConverter.Convert.


You may be able to pass bindable parameters to your valueconverter if you implement IMultiValueConverter and bind the parameters using the MultiBinding class in XAML:

<SomeFrameworkElement>
    <SomeFrameworkElement.SomeProperty>
        <MultiBinding Converter="{StaticResource someConverter}" >
            <Binding Path="DataContext"/>
        </MultiBinding>        
    </SomeFrameworkElement.SomeProperty>
</SomeFrameworkElement>

The binding elements of the <MultiBinding> element are passed to the Convert method of IMultiValueConverter as the values parameter. Convert has the following signature:

public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture);
没︽人懂的悲伤 2024-09-11 08:30:46
SomeProperty="{Binding . ,Converter={StaticResource SomeConverter}, ConverterParameter=someParam}"

点表示您绑定到数据上下文

SomeProperty="{Binding . ,Converter={StaticResource SomeConverter}, ConverterParameter=someParam}"

The dot is telling that you binding to datacontext

紫南 2024-09-11 08:30:46

这是一种简单而有效的方法(适用于我的应用程序):

<DataGrid.Columns>
    <DataGridTextColumn Width="*" Header="ColumnHeader">
        <DataGridTextColumn.Binding>
            <Binding Converter="{StaticResource YourConverterKey}" ConverterParameter="DataContext"/>
        </DataGridTextColumn.Binding>
    </DataGridTextColumn>

现在您可以在 Convertor 方法中使用 (value) 作为您的 DataContext

Here is a simple and efficient way (that works for my app):

<DataGrid.Columns>
    <DataGridTextColumn Width="*" Header="ColumnHeader">
        <DataGridTextColumn.Binding>
            <Binding Converter="{StaticResource YourConverterKey}" ConverterParameter="DataContext"/>
        </DataGridTextColumn.Binding>
    </DataGridTextColumn>

Now you can use (value) in the Convertor method as your DataContext.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文