【问题标题】:DataContext as Source for Converter Binding Within ResourcesDataContext 作为资源内转换器绑定的源
【发布时间】:2011-02-28 17:54:12
【问题描述】:

 <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 的根视觉资源中?

【问题讨论】:

  • 由于某种原因,stackoverflow 截断了我的样式和控件模板。此 Canvas 位于控件模板的根目录。

标签: wpf binding datacontext controltemplate ivalueconverter


【解决方案1】:
SomeProperty="{Binding . ,Converter={StaticResource SomeConverter}, ConverterParameter=someParam}"

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

【讨论】:

    【解决方案2】:

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

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

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

    【讨论】:

    • 这会在转换器中提供文本DataContext
    【解决方案3】:

    以前的答案真的很接近。但是多重绑定中的绑定应该是:

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

    对我有用

    【讨论】:

    • 在我的情况下抛出异常“双向绑定需要 Path 或 XPath。”。我正在绑定 TextBox 的 Text 属性。
    【解决方案4】:

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

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

    然后,数据上下文将作为参数传递给您的值转换器,以实现IValueConverter.Convert


    如果您实现 IMultiValueConverter 并使用 XAML 中的 MultiBinding 类绑定参数,您可能能够将可绑定参数传递给您的值转换器:

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

    &lt;MultiBinding&gt; 元素的绑定元素作为values 参数传递给IMultiValueConverterConvert 方法。 Convert 具有以下签名:

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

    【讨论】:

    • 不幸的是,您不能在“ConverterParameter”上使用绑定,但它会非常方便。
    • 你当然是完全正确的。但我认为您可以为此使用多重绑定。我相应地更新了我的答案。
    • 我还没有机会对此进行测试,因为我一直在从事其他一些项目。但如果它解决了我的问题,我一定会给予你信任。
    • 我只是想感谢你为我指明了使用 IMultiValueConverter 接口的方向。
    猜你喜欢
    • 1970-01-01
    • 2011-09-06
    • 1970-01-01
    • 1970-01-01
    • 2023-03-20
    • 2011-07-21
    • 2013-01-20
    • 2013-05-13
    • 1970-01-01
    相关资源
    最近更新 更多