Bind Converter to the parent resource in xaml - wpf

I have a usercontrol1.xaml where I have a resource defined:
<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>
Then in the user control view 2, I want to have this situation:
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>
I was able to link to the parent control for the ItemSource, but for the converter how can I do it?. I can not event move the resource definition from control1 to control2. Inside of the RadTileViewItem is not possible to add another resource. Exactly in usercontrol1View I have a tabcontrol inside the RadTileViewItem and inside the tabiteam i have inlcluded the UserControl2View.
How can I link to the parent resource for the covnerter?

How can I link to the parent resource for the covnerter?
You can't. If you need to use the same converter in both UserControls, you have defined the resource in the wrong place actually.
You could either move it to your App.xaml file:
<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>
Then you will be able to reference it across your entire application. The other option would be to define another resource of the same type in UserControl2:
<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>
But you can't reference a resource that is defined in a parent element using bindings.

I suppose you wanted bind converter object to the binding's Converter property.
You can't bind to the binding's Converter property since it is not a ´DependencyProperty´. You can acces a resource object and bind it e.g. to the ´Tag´, but it doesn't solve your problem:
<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">
If object is nested, you could just set(not bind) the Converter to the resource object:
Converter = {StaticResource demoManagerConverter}

Related

In wpf how to access window.resources in usercontrol

I have a child usercontrol and I am having trouble accessing its parent usercontrol's resources. The resource is a collectionViewSource and its source is set in the code behind of the parent usercontrol, the source is set to a query result from LINQ to SQL. Both usercontrols are defined in their own xaml file. I tried using DynamicResource and it complained saying it needs to be used with a DepenednecyProperty or so...
parent usercontrol:
...
<UserControl.Resources>
<CollectionViewSource x:Key="UsageTypeSource"/>
</UserControl.Resources>
...
child usercontrol:
...
<ComboBox Height="28" HorizontalAlignment="Left" Margin="147,1,0,0" x:Name="cbxUsage"
Grid.Column="1" Grid.Row="2" Width="186"
SelectedItem="{Binding Path=UsageType, ValidatesOnExceptions=true, NotifyOnValidationError=true}"
VerticalAlignment="Top" SelectedValuePath="ID"
SelectedValue="{Binding Path=Usage}"
ItemsSource="{Binding Source={StaticResource UsageTypeSource}}"
DisplayMemberPath="Type"/>
...
The error I am getting is 'The resource "UsageTypeSource" could not be resolved'
Can you provide the proper way to access the resource
Thanks in advance
Ash
Try this:
ItemsSource="{DynamicResource UsageTypeSource}}"/>
Instead of
<UserControl.Resources> ... </UserControl.Resources>
Use
<Control.Resources> ... </Control.Resources>

Binding one dependency property to another

I have a custom Tab Control that I have created, but I am having an issue. I have an Editable TextBox as part of the custom TabControl View.
<Controls:EditableTextControl x:Name="PageTypeName"
Style="{StaticResource ResourceKey={x:Type Controls:EditableTextControl}}" Grid.Row="0" TabIndex="0"
Uid="0"
AutomationProperties.AutomationId="PageTypeNameTextBox"
AutomationProperties.Name="PageTypeName"
Visibility="{Binding ElementName=PageTabControl,Path=ShowPageType}">
<Controls:EditableTextControl.ContextMenu>
<ContextMenu x:Name="TabContextMenu">
<MenuItem Header="Rename Page Type" Command="{Binding Path=PlacementTarget.EnterEditMode, RelativeSource={RelativeSource AncestorType=ContextMenu}}"
AutomationProperties.AutomationId="RenamePageTypeMenuItem"
AutomationProperties.Name="RenamePageType"/>
<MenuItem Header="Delete Page Type" Command="{Binding Path=PageTypeDeletedCommand}"
AutomationProperties.AutomationId="DeletePageTypeMenuItem"
AutomationProperties.Name="DeletePageType"/>
</ContextMenu>
</Controls:EditableTextControl.ContextMenu>
<Controls:EditableTextControl.Content>
<!--<Binding Path="CurrentPageTypeViewModel.Name" Mode="TwoWay"/>-->
<Binding ElementName="PageTabControl" Path="CurrentPageTypeName" Mode ="TwoWay"/>
</Controls:EditableTextControl.Content>
</Controls:EditableTextControl>
In the Content section I am binding to a Dependency Prop called CurrentPageTypeName. This Depedency prop is part of this custom Tab Control.
public static DependencyProperty CurrentPageTypeNameProperty = DependencyProperty.Register("CurrentPageTypeName", typeof(object), typeof(TabControlView));
public object CurrentPageTypeName
{
get { return GetValue(CurrentPageTypeNameProperty) as object; }
set { SetValue(CurrentPageTypeNameProperty, value); }
}
In another view, where I am using the custom TabControl I then bind my property, with the actual name value, to CurrentPageTypeName property as seen below:
<Views:TabControlView Grid.Row="0" Name="RunPageTabControl"
TabItemsSource="{Binding RunPageTypeViewModels}"
SelectedTab="{Binding Converter={StaticResource debugConverter}}"
CurrentPageTypeName="{Binding Path=RunPageName, Mode=TwoWay}"
TabContentTemplateSelector="{StaticResource tabItemTemplateSelector}"
SelectedIndex="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, Path=DataContext.SelectedTabIndex}"
ShowPageType="Hidden" >
<!--<Views:TabControlView.TabContentTemplate>
<DataTemplate DataType="{x:Type ViewModels:RunPageTypeViewModel}">
<RunViews:RunPageTypeView/>
</DataTemplate>
</Views:TabControlView.TabContentTemplate>-->
</Views:TabControlView>
My problem is that nothing seems to be happening. It is grabbing its Content from the Itemsource, and not from my chained Dependency props. Is what I am trying even possible? If so, what have I done wrong.
Thanks for looking.
Unless I'm missing something this is definitely possible. Here is a simplified working example.
User control with a dependency property named TestValue, containing a TextBox bound to this property:
<UserControl x:Class="TestApp.TestControl" 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:d="http://schemas.microsoft.com/expression/blend/2008" mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="300"
x:Name="TestControlName">
<Grid>
<TextBox Text="{Binding ElementName=TestControlName, Path=TestValue, Mode=TwoWay}"/>
</Grid>
</UserControl>
A different view using this user control, binding the above mentioned dependency property to something:
<Window x:Class="TestApp.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:TestApp="clr-namespace:TestApp" Title="MainWindow"
Height="350" Width="525">
<StackPanel>
<TestApp:TestControl TestValue="{Binding ElementName=SourceTextBox, Path=Text, Mode=TwoWay}" />
<TextBox Name="SourceTextBox" />
</StackPanel>
</Window>
It sounds that the issue is somewhere in the part of the code you have not posted (e.g. wrong name used in Content binding).
I think you already solved this yourself for the "SelectedIndex" property. Just do the same thing for the "CurrentPageType" property i.e. use RelativeSource

Caliburn Micro: ElementName for ComboBox in DataGrid not working

my problem is that the ItemsSource of a ComboBox in an DataGrid is not binding.
My UserControl:
<UserControl x:Class="MyClass"
x:Name="Root"
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:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:cal="clr-namespace:Caliburn.Micro;assembly=Caliburn.Micro"
mc:Ignorable="d"
d:DesignHeight="360" d:DesignWidth="757">
The DataGrid:
<DataGrid x:Name="Article"
AutoGenerateColumns="False"
SelectionUnit="FullRow"
SelectionMode="Single"
CanUserAddRows="False"
CanUserDeleteRows="True"
Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="4"
Margin="5">
<DataGrid.Columns>
<DataGridTemplateColumn Width="*"
Header="Article">
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding FilteredArticle, ElementName=Root}" IsEditable="True"
cal:Message.Attach="[Event KeyUp] = [Action FindArticlel($source)]" />
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>
If I don't say ElementName=Root, he tries to bind FilteredArticle in the Article class.
If I say ElementName=Root, he says the following at runtime:
System.Windows.Data Error: 4 : Cannot find source for binding with reference 'ElementName=Root'. BindingExpression:Path=FilteredArticle; DataItem=null; target element is 'ComboBox' (Name=''); target property is 'ItemsSource' (type 'IEnumerable')
FilteredArticle is a BindableCollection in my ViewModel. All other bindings are working.
What is wrong here?
Using the latest stable version of Caliburn.Micro.
Binding to the view via ElementName is usually a bad practice. Mainly because someone that creates an instance of the view can give it a different x:Name and that will break your internal binding.
Plus, the FilteredArticle property is not the property of the view but of the view model which is the data context for the view.
Use relative source for binding in those scenarios
ItemsSource="{Binding DataContext.FilteredArticle,
RelativeSource={RelativeSource FindAncestor,
AncestorType={x:Type UserControl}}}"
You can use a more specific notation (although, in 99% of cases it's not necessary)
ItemsSource="{Binding DataContext.FilteredArticle,
RelativeSource={RelativeSource FindAncestor,
AncestorType={x:Type local:MyClass}}}"
Where local is the xmlns for the namespace of MyClass

Get UserControl DataContext properties to bind to from within the control

Here is the code for me UserControl:
<UserControl x:Class="UZ.ActivitySink.GUI.Views.POSsView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Views="clr-namespace:UZ.ActivitySink.GUI.Views">
<DockPanel>
<TreeView ItemsSource="{Binding Types}" x:Name="POSTree" Background="{x:Null}" HorizontalAlignment="Left" FontSize="14"
Visibility="{Binding DataContext.TreeVisibility, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Views:POSsView}}, Mode=TwoWay}">
</TreeView>
<StackPanel x:Name="ErrorPanel"
Visibility="{Binding DataContext.ErrorMessageVisibility, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Views:POSsView}}, Mode=TwoWay}" Margin="20">
</StackPanel>
</DockPanel>
</UserControl>
I am assigning a datacontext object to the control in it's constructor
DataContext = _viewModel;
_viewModel has the properties named TreeVisibility and ErrorMessageVisibility of type Visibility, but still the Visual elements on the screen don't bind their visibility values to these properties.
What is the correct way to reference the controls' viewmodel properties from xaml declaration in my case?
Thank you.
Your bindings are more complicated than necessary.
This one:
Visibility="{Binding DataContext.TreeVisibility,
RelativeSource={RelativeSource FindAncestor,
AncestorType={x:Type Views:POSsView}}, Mode=TwoWay}"
in this case should be equivalent to the much simpler
Visibility="{Binding TreeVisibility}"
That said, even though the current bindings are complicated they should still have worked (at least given the information you already provide).
If you still can't get them to work, run your app in the debugger and look at the Output window -- binding errors are reported there by default, and they contain information that will help you get to the root of the problem.

Binding to ancestors from within a ResourceDictionary

How can I bind to a UserControl's property from within its ResourceDictionary? I want an object I declare in my resources to have the same DataContext as the UserControl it is contained in:
<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Some.Namespace"
DataContext="{Binding Path=ViewModel, RelativeSource={RelativeSource Self}}">
<UserControl.Resources>
<local:SomeClass
x:Key="SomeClass"
DataContext="{Binding Path=DataContext, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}" />
</UserControl.Resources>
</UserControl>
At runtime I get the error:
System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='UserControl', AncestorLevel='1''. BindingExpression:Path=DataContext; DataItem=null; target element is 'SomeClass' (Name=''); target property is 'DataContext' (type 'Object')
My workaround was to set the DataContext of the resource in the code-behind.
.xaml
<local:SomeType x:Key="SomeKey" SomeProperty="{Binding ... }" />
.xaml.cs
public SomeControl()
{
InitializeComponent();
((SomeType)this.Resources["SomeKey"]).DataContext = this;
}
When using FindAncestor, the target element needs to be a descendent (either logical or visual) of the source. Your object does not appear in either the visual or logical tree, it's simply in the resources. So you can't use RelativeSource with FindAncestor for your object.
You can use ElementName in your Binding though. Something like this should work:
<UserControl x:Name="userControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Some.Namespace"
DataContext="{Binding Path=ViewModel, RelativeSource={RelativeSource Self}}">
<UserControl.Resources>
<local:SomeClass
x:Key="SomeClass"
DataContext="{Binding Path=DataContext, ElementName=userControl}" />
</UserControl.Resources>
</UserControl>
Set x:Shared="False", this will clone resource on each use and make it a child of your element, enabling usage of bindings.
<local:SomeClass
x:Key="SomeClass"
x:Shared="False"
DataContext="{Binding Path=DataContext, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}" />
I think what you're looking for is just {Binding} which binds to the inherited DataContext. Here's an example, though a bit strange shows how you can grab a color through binding to the DataContext:
<Window x:Class="AncestorBinding.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Window.Resources>
<SolidColorBrush x:Key="MyBrush" Color="Blue" />
</Window.Resources>
<StackPanel>
<Button DataContext="{Binding Source={StaticResource MyBrush}}" Content="My Button">
<Button.Resources>
<Style TargetType="{x:Type Button}">
<Setter Property="Background" Value="{Binding}" />
</Style>
</Button.Resources>
</Button>
</StackPanel>
</Window>
What I would do is to create an attached behavior (ContextualizeResourceBehavior) on the user control, and specify the resource key on that attached behavior. The behavior would lookup the resource (not sure you would be able to do it on attach, if not you would need to hook up Loaded event) and transfer the data context.
when you add your resource to the visual tree it should inherit the data context. but... have a look at element spy it might just do what you need.

Resources