In wpf how to access window.resources in usercontrol - wpf

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>

Related

Bind Converter to the parent resource in xaml

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}

Unable to get SelectedItems through Xceed CheckListBox using MVVM

I have been using wpf xceed third party library for some of the UI components. I really like the way CheckListBox is being displayed at the screen. But I am not able to get the selectedItems bound to any property in the viewmodel (the setter never triggers). Here is the code -
I am using a dataprovider to get values from enum -
<UserControl.Resources>
<ObjectDataProvider MethodName="GetValues" ObjectType="{x:Type sys:Enum}" x:Key="DeviceClassDataProvider">
<ObjectDataProvider.MethodParameters>
<x:Type TypeName="Model:HANDeviceClass" />
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
And then the control has been declared something like this -
<ext:CheckListBox Focusable="False" SelectedMemberPath="{Binding IsChecked, UpdateSourceTrigger=PropertyChanged}" SelectedItemsOverride="{Binding SelectedDeviceGroups, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" SelectedItem="{Binding SelectedItem, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Grid.Row="1" Grid.RowSpan="7" Grid.Column="4" Padding="5" BorderThickness="0.8" BorderBrush="Gray" ItemsSource="{Binding Source={StaticResource DeviceClassDataProvider}}"/>
How will i get the selected items in it's viewmodel?
Any quick help would be highly appreciated!
Thanks in advance
Should work provided that SelectedDeviceGroups is a public property that returns an ICollection<HANDeviceClass>:
public ICollection<HANDeviceClass> SelectedDeviceGroups { get; } = new ObservableCollection<HANDeviceClass>();
XAML:
<ext:CheckListBox ItemsSource="{Binding Source={StaticResource DeviceClassDataProvider}}"
SelectedItemsOverride="{Binding SelectedDeviceGroups}" />
<TextBlock Text="{Binding SelectedDeviceGroups.Count}" />
Items will be added to and removed from the source collection as you check and uncheck items respectively.

WPF Itemscontrol datatemplate property changing

I'm currently using the following itemscontrol and datatemplate:
<UserControl.Resources>
<DataTemplate x:Key="OrdersTemplate">
<dxlc:LayoutItem Label="CustomerReference" LabelPosition="Top" MaxWidth="300" HorizontalAlignment="Left" Width="300">
<dxe:TextEdit IsEnabled="True" Text="{Binding Path=CustomerReference}" />
</dxlc:LayoutItem>
</DataTemplate>
</UserControl.Resources>
<HeaderedContentControl Header="Steps">
<ItemsControl ItemsSource="{Binding Orders}" ItemTemplate="{StaticResource OrdersTemplate}"/>
</HeaderedContentControl>
The source is just a list with entities.
The problem is that the "CustomerReference" of every object from my source changes when I change it in the textbox. Whats missing here?
Greets
I think in your view model you have added the same object more than once. Instead of creating new object when ever you added to the collection. So when you want to add a object to collection create a new object and add it

WPF Element Binding in a within a Resource control doesnt work

I have some xaml that will just copy text from one text box to another:
<StackPanel Orientation="Horizontal">
<TextBox Width="100" Height="30" Text="{Binding ElementName=src1, Path=Text}" />
<TextBox x:Name="src1" Width="100" Height="30" />
</StackPanel>
Nothing special, works fine. A bit dumb but just an example.
However if I put the StackPanel as a resource in the Window and create dynamically from code, like this:
<Window.Resources>
<StackPanel x:Key="MySP" Orientation="Horizontal">
<TextBox Width="100" Height="30" Text="{Binding ElementName=src, Path=Text}"/>
<TextBox x:Name="src" Width="100" Height="30" />
</StackPanel>
</Window.Resources>
.. then the element binding doesnt work anymore.
Why? and how to make it work? Any ideas gratefully received.
The following Xaml should work just fine
<Window ...>
<Window.Resources>
<StackPanel x:Key="MySP" Orientation="Horizontal">
<TextBox Width="100" Height="30" Text="{Binding ElementName=src, Path=Text}"/>
<TextBox x:Name="src" Width="100" Height="30" />
</StackPanel>
</Window.Resources>
<StaticResource ResourceKey="MySP"/>
</Window>
You could also use it from code
StackPanel mySP = TryFindResource("MySP") as StackPanel;
if (mySP != null)
{
this.Content = mySP;
}
However, what is the reason for you to have a StackPanel in the Windows Resoures?
If you want to be able to reuse it several times you would have to set x:Shared="False" on the Resource but then you'll get an exception saying something like Cannot register duplicate Name 'src' in this scope the second time you add it.
As far as I'm concerned you should not put that in <Window.Resources>. Only styles, static, dynamic resources and such...
http://msdn.microsoft.com/en-us/library/ms750613.aspx
<Window>
<Window.Resources>
</Window.Resources>
<StackPanel x:Key="MySP" Orientation="Horizontal">
<TextBox Width="100" Height="30" Text="{Binding ElementName=src, Path=Text}"/>
<TextBox x:Name="src" Width="100" Height="30" />
</StackPanel>
<Window>
Having a similar issue, trying to get relative binding to my source control - In my case, I'm creating a designer and need the element as a static so styles can use it's dimensions for centering calculations on a canvas.
Taking a line from [WPF Xaml Namescopes],
ResourceDictionary does not use XAML names or namescopes ; it uses keys instead, because it is a dictionary implementation.
So, directly using ElementName in a resource Dictionary simply does not work, because no name will bind without a NameScope. Also attempted reproducing your situation with Style setters, but no luck - one cannot set an object's name via a Style.
Now, the convoluted solution I cam up with is to
Create a DependencyProperty in the code-behind of the class
you're declaring this resource in.
replace ElementName=Root with RelativeSource={RelativeSource
FindAncestor, AncestorType={x:Type namespace:RootClass}} and bind
to said container directly, allowing you to bind to said
DependencyProperty whilst bypassing the names.
If you need bindings to operate between two elements in the same
StaticResource, bind the source to said DependencyProperty as
OneWayToSource or TwoWay, and the destination as OneWay or TwoWay.
1

WPF UserControl Binding Problem

I have the following user control that is embedded within another user control.
<UserControl.Resources>
<DataTemplate x:Key="ContextsTemplate">
<Label Margin="10,0,20,0" Content="{Binding Name}"/>
</DataTemplate>
</UserControl.Resources>
<ItemsControl Name="Contexts"
Background="Transparent"
ItemsSource="{Binding}"
Margin="0,0,0,0"
VerticalAlignment="Center"
AlternationCount="2"
ItemTemplate="{StaticResource ContextsTemplate}">
</ItemsControl>
Here is XAML code for the user control (controlB) above that is embedded in another user control (controlA).
<local:ContextContentsUserControl Height="30" Content="{Binding Contexts}"/>
controlA is displayed on the screen as "(Collection)" but for some reason doesn't show each item in the collections text in the label. Please help.
The problem is here:
Content="{Binding Contexts}"
You meant:
DataContext="{Binding Contexts}"
The reason you got "(Collection)" instead of the content you had defined for controlA is the content you defined in the XAML for ControlA was replaced with your collection. The body of a UserControl's XAML file simply sets its Content property: You replaced it after it was set.
When you declare your ContextContentsUserControl, you are setting its Content property. You need to be setting the DataContext instead:
<local:ContextContentsUserControl Height="30" DataContext="{Binding Contexts}" />

Resources