Wpf 将转换器绑定到xaml中的父资源
我有一个usercontrol1.xaml,其中定义了一个资源:Wpf 将转换器绑定到xaml中的父资源,wpf,xaml,data-binding,relativesource,Wpf,Xaml,Data Binding,Relativesource,我有一个usercontrol1.xaml,其中定义了一个资源: <UserControl x:Class="FrameworkDemo.usercontrol1View"> <UserControl.Resources> <local:DemoManger x:Key="demoManager"/> <local:DemoManagerConverterx x:Key="demoMan
<UserControl x:Class="FrameworkDemo.usercontrol1View">
<UserControl.Resources>
<local:DemoManger x:Key="demoManager"/>
<local:DemoManagerConverterx x:Key="demoManagerConverter" Manager="{StaticResource strategyManager}"/>
</UserControl.Resources>
<telerik:RadTileView MinimizedItemsPosition="Top">
<telerik:RadTileViewItem>
<local:UserControl2View/>
</telerik:RadTileViewItem>
<telerik:RadTileViewItem>
........
</telerik:RadTileViewItem>
</telerik:RadTileView>
</UserControl>
........
然后在用户控件视图2中,我希望出现以下情况:
ss
<UserControl x:Class="FrameworkDemo.usercontrol2View">
<DockPanel>
<ComboBox MinWidth="270" Margin="0,0,5,5"
ItemsSource="{Binding Path=Demos, RelativeSource={RelativeSource AncestorType={x:Type local:DemoManager}}}"
SelectedValue="{Binding Path=CurrentStrategy, Converter={ ????}, Mode=TwoWay}"
IsEnabled="{Binding CanRefreshExecutionList, ElementName=Instance}"
DropDownOpened="StrategyComboBox_DropDownOpened">
</DockPanel>
</UserControl>
ss
我能够链接到ItemSource的父控件,但是对于转换器,我如何才能做到?。我无法将资源定义从control1移动到control2。在RadTileViewItem内部无法添加其他资源。确切地说,在usercontrol1View中,我在RadTileViewItem中有一个tabcontrol,在tabiteam中,我包括UserControl2View
如何链接到cownerter的父资源?我想您希望将converter对象绑定到绑定的
converter
属性。无法绑定到绑定的
转换器
属性,因为它不是“DependencyProperty”。您可以访问资源对象并将其绑定(例如绑定到“标记”),但这并不能解决您的问题:
<ComboBox MinWidth="270" Margin="0,0,5,5"
ItemsSource="{Binding Path=Demos, RelativeSource={RelativeSource AncestorType={x:Type local:DemoManager}}}"
SelectedValue="{Binding Path=CurrentStrategy, Converter={ ????}, Mode=TwoWay}"
Tag="{Binding Path='Resources[demoManagerConverter]', RelativeSource={RelativeSource AncestorType={x:Type localFrameworkDemo:usercontrol1View}}}"
IsEnabled="{Binding CanRefreshExecutionList, ElementName=Instance}"
DropDownOpened="StrategyComboBox_DropDownOpened">
如何链接到covnerter的父资源
你不能。如果您需要在两个UserControls
中使用相同的转换器,那么实际上您在错误的位置定义了资源
您可以将其移动到App.xaml
文件:
<Application x:Class="WpfApp1.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApp1"
StartupUri="MainWindow.xaml">
<Application.Resources>
...
<local:DemoManagerConverterx x:Key="demoManagerConverter" Manager="{StaticResource strategyManager}"/>
</Application.Resources>
</Application>
但是不能使用绑定引用父元素中定义的资源。我不太清楚。为什么不使用转换器的新实例为第二个控件声明一个资源字典?这是唯一的解决方案吗?只为转换器定义一个目录是一个糟糕的解决方案。你称之为“新目录”是什么?你是说一个新的例子?这样做绝对没有问题。您甚至可以绑定到一个单例实例……它无法找到demoManagerConverter
<Application x:Class="WpfApp1.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApp1"
StartupUri="MainWindow.xaml">
<Application.Resources>
...
<local:DemoManagerConverterx x:Key="demoManagerConverter" Manager="{StaticResource strategyManager}"/>
</Application.Resources>
</Application>
<DockPanel>
<DockPanel.Resources>
<local:DemoManagerConverterx x:Key="demoManagerConverter" Manager="{StaticResource strategyManager}"/>
</DockPanel.Resources>
<ComboBox MinWidth="270" Margin="0,0,5,5"
ItemsSource="{Binding Path=Demos, RelativeSource={RelativeSource AncestorType={x:Type local:DemoManager}}}"
SelectedValue="{Binding Path=CurrentStrategy, Converter={StaticResource demoManagerConverter}, Mode=TwoWay}"
IsEnabled="{Binding CanRefreshExecutionList, ElementName=Instance}"
DropDownOpened="StrategyComboBox_DropDownOpened" />
</DockPanel>