I have UserControl which needs to perform some multi-linguality. In constructor, I dynamically load proper XAML with ResourceDictionary. In that XAML I have:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:system="clr-namespace:System;assembly=mscorlib">
<system:String x:Key="somekey">Some Label</system:String></ResourceDictionary>
I then have
Header = {DynamicResource somekey}
in DataGridTextColumn in DataTemplate which is also used by this UserControl. It looks like the DataTemplate doesn't know about the new ResourceDictionary.
But, it does not work. Please, why?
I guess you should bind the (re)source to the header content. The resource itself cannot notify any change, so the binding may be the solution.
Header={Binding Source={DynamicResource someKey}, Path=.}
Anyway I'm not sure: never try such a condition.
Related
In WPF, would it be possible for a ResourceDictionary (Child.xaml) to inherit from another ResourceDictionary (Base.xaml) using only XAML notation.
I tried using the tag ResourceDictionary and then try to see if some kind of merge or basedOn was available, but I couldn't find anything.
For example, I have the following (assume) child ResourceDictionary (Child.xaml file):
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:core="clr-namespace:System;assembly=mscorlib">
</ResourceDictionary>
Is it possible from there derive from another base ResourceDictionary using basedOn or something like that?
I am trying to make an InkToolbar whose ballpens and pencils' size can't be changed. Basically, the actual appearance is this one (the button has a slider with a "size" text) and I want to remove it so it looks like this, without that slider. I've read the whole tutorial about custom ink toolbars, but I haven't been able to find the property that I want to remove. In fact, I'm not sure if I can do so.
Is there any way to prevent users from seeing and using this size property?
You can custom InkToolbarPenConfigurationControl located in Generic.xaml in your pc.
For details.
Step 1, open Generic.xaml, find text <Style TargetType="InkToolbarPenConfigurationControl">, copy out the whole Style, you can also see on my gist.
Step 2, paste the style code to your App.xaml. Note this will override all your InkToolbarPen button.
<Application
x:Class="App2.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:App2">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/FavoriteButtonStyle.xaml"/>
<ResourceDictionary Source="/BorderlessGridViewItem.xaml"/>
</ResourceDictionary.MergedDictionaries>
<!--Paste your style code here-->
</ResourceDictionary>
</Application.Resources>
</Application>
Step 3, find the Slider control, set Visibility to Collapsed. Done!!!
Why can't my custom ListView have it's own xaml file? I have a custom Button and it works with a xaml file with no issues, but not my ListView. The main reason I want to use this approach (rather than be forced to create a Style that is place in the Generic.xaml file) is because I would like to take advantage of the Resources element and place all resources related to the listview within the xaml file:
public sealed partial class MyListView : ListView
{
public MyListView()
{
this.InitializeComponent();
}
}
And here is the associated xaml file:
<ListView
x:Class="App1.MyListView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:App1"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300"
d:DesignWidth="400">
<ListView.Resources>
<!-- I would like to place all related resources here instead of having
them placed in external locations, and then have to open different files to find them. -->
</ListView.Resources>
</ListView>
Although I would expect this should work as well, it seems that this problem is present and it is recommended to use the templated control instead.
I suppose the problem is that assigning the compiler is unable to generate the valid assignment to the Items property of the control, which it is trying to construct from the content of the element. Even when the element is closed immediately it seems to be an issue.
Why not place resources on the Page or inside ListView, rather than deriving your own control?
<Page
x:Class="ListViewResources.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:ListViewResources"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Page.Resources>
<!-- Place all related resources here instead of having them placed in external locations, and then have to open different files to find them. -->
</Page.Resources>
<ListView x:Name="MyListView">
<ListView.Resources>
<!-- Or place related resources here -->
</ListView.Resources>
</ListView>
</Page>
I have XAML like this which lets me add in a 3rd Party map control
<UserControl x:Class="AssemblyName.Views.CustomMapView"
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:local="clr-namespace:AssemblyName.Views"
xmlns:ioc="clr-namespace:AssemblyName.Ioc"
xmlns:esri="http://schemas.esri.com/arcgis/runtime/2013"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300"
ioc:ViewModelLocator.AutoWireViewModel="True">
<esri:MapView x:Name="customMapView">
<esri:Map x:Name="customMap">
<esri:ArcGISTiledMapServiceLayer ID="BaseMap" ServiceUri="http://services.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer"/>
</esri:Map>
</esri:MapView>
</UserControl>
All my business logic which happens in the ViewModel, needs to interact with this control and make it do things. Ideally, I would like the the View to have no idea of what type of control it is. I do this all the time with UserControls by making an entry in the XAML like:
<ContentControl Name="menuControl" Content="{Binding MenuControl}"/>
then the ViewModel can set whatever "Menu Control" object that inherits from ContentControl.
since the customMapView doesn't inherit from ContentControl, I can't use the method above that I normally use. It inherits from Control.
Is there a way I can put in a standard <control/> and assign my map control to it?
Basically, I just want to interact with this Map object in the ViewModel in the most decoupled way possible.
As stated on the Content property description of the ContentControl class :
Because the Content property is of type Object, there are no
restrictions on what you can put in a ContentControl.The Content is
displayed by a ContentPresenter, which is in the ControlTemplate of
the ContentControl. Every ContentControl type in WPF has a
ContentPresenter in its default ControlTemplate
So you can still bind your Content property like you used to :
<ContentControl Content="{Binding MyDisplayedControl}"/>
Why not just inherit from ContentControl? It obviously has content.
Then Create a template for your control which contains a ContentControl who is bound by
<ContentControl Content="{Binding RelativeSource="{RelativeSource Mode=TemplatedParent}, Path=Content}"/>
I think thats right anyhow.. i'm on my phone. Man I love Intellisense..
Anyhow, Specifically setting the content of a content control to another control seems redundant.
I'm trying to access a resource dictionary in a UserControl code-behind via C# and I'm having little success.
Merged Dictionary:
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Resources/BiometricDictionary.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
Embedded Dictionary:
<UserControl.Resources>
<BitmapImage x:Key="imageDefault">/Resources/Images/default_thumb.png</BitmapImage>
<BitmapImage x:Key="imageDisconnected">/Resources/Images/disconnect_thumb.png</BitmapImage>
<BitmapImage x:Key="imageFailed">/Resources/Images/failed_thumb.png</BitmapImage>
<BitmapImage x:Key="imageSuccess">/Resources/Images/success_thumb.png</BitmapImage>
</UserControl.Resources>
Code behind:
var resourceDictionary = new ResourceDictionary();
resourceDictionary.Source = new Uri("/Resources/BiometricDictionary.xaml", UriKind.Relative);
I've tried all of the examples and helpful tips but coming up short. Right now, success would be the ability to load the dictionary. Any suggestions?
To access one of your UserControl's XAML resources in your codebehind, all you need to do is access the Resources property of the UserControl. Something like this:
BitmapImage myImage = (BitmapImage)this.Resources["imageDefault"];
Though, the preferred method is to use FindResource(), which will search the entire logical tree for a match to the key, rather than just the object it is called on.
BitmapImage myImage = (BitmapImage)this.FindResource("imageDefault");
Try to remove the forward slash infront of your location. The only time you should use /Resources is if you have to go up a library first. like ../Resources
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Resources/BiometricDictionary.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
Hope this helps you.
So, you have a ResourceDictionary defined in a UserControl's assembly, and would like to access it from that UserControl's code-behind?
You should be able to. However, if the code you listed is in the constructor, you may not have access to the resource dictionary (might not be loaded yet). Try adding that same code to your UserControl's "loaded" event, and see if that works. If you're simply trying to access a resource, such as a style or template, using the "FindResource" or "TryFindResource" functions directly from your class should work as well (i.e. you don't need to have an object of type "ResourceDictionary").
Hope that helps!
d'Oh...after compiling to the local bin so that references are relative, I implemented the pack URI solution found here: ResourceDictionary in a separate assembly and then FindResource(x:key value here).
#PeterAllenWeb, #Pwninstein, thanks for your quick responses and getting me thinking again.