I want to bind an enum to the ComboBox.
Here is the class BarcodeModel.cs which contains the enum:
And here is the enum in the class:
public enum BarcodeType
{ AZTEC, CODABAR, CODE128, CODE93, CODE39, DATA_MATRIX, EAN13, EAN8, ITF, MAXICODE, PDF417, QRCODE, RSS14, RSSEXPANDED, UPCA, UPCE, UPC_EAN_EXTENSION }
Well, in another directory of the same project I use an ObjectDataProvider to bind it:
<Page x:Class="KongGamLung.ToolProperty.BarCodeProperty"
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:KongGamLung.ToolProperty"
xmlns:System="clr-namespace:System;assembly=mscorlib"
xmlns:Model="clr-namespace:KongGamLung.Models"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800"
Title="BarCodeProperty">
<Page.Resources>
<ObjectDataProvider x:Key="dataFromEnum" MethodName="GetValues"
ObjectType="{x:Type System:Enum}">
<ObjectDataProvider.MethodParameters>
<x:Type TypeName="Model:BarcodeModel.BarcodeType"/>
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
</Page.Resources>
<ComboBox x:Name="BarcodeTypeCB" ItemsSource="{Binding Source={StaticResource dataFromEnum}}"></ComboBox>
</Page>
After it ran, Visual Studio threw a XamlParseException error which can not find “{clr-namespace:KongGamLung.Models;assembly=KongGamLung, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null}BarcodeModel.BarcodeType”
How can I reference the enum correctly? Would you please help me? Thank you.
If you want to reference a nested type in xaml, instead of '.', use '+'.
<x:Type TypeName="Model:BarcodeModel+BarcodeType"/>
Related
I use MaterialDesign in my app and I would like to give an alias to the PrimaryHueLightBrush SolidColorBursh, which is defined there. I have found that StaticResourceExtension seems to be the solution to this. But I have not been able to get this to work.
Also I would like the "alias" to be defined in the App.xaml so the alias will be accessible from my entire application.
I have defined the "alias" in the App.xaml and I use it in the MainWindow.xaml.
App.xaml
<Application x:Class="MaterialDesignTest.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:MaterialDesignTest"
xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
StartupUri="MainWindow.xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<materialDesign:BundledTheme BaseTheme="Light" PrimaryColor="DeepPurple" SecondaryColor="Lime" />
<ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Defaults.xaml" />
</ResourceDictionary.MergedDictionaries>
<StaticResourceExtension x:Key="MyAlias" ResourceKey="PrimaryHueLightBrush" />
</ResourceDictionary>
</Application.Resources>
MainWindow
<Window x:Class="MaterialDesignTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:MaterialDesignTest"
xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
mc:Ignorable="d"
Background="{DynamicResource MaterialDesignPaper}"
TextElement.Foreground="{DynamicResource MaterialDesignBody}"
Title="MainWindow" Height="Auto" Width="500">
<Border Background="{DynamicResource MyAlias}" />
</Window
In the designer of my MainWindow I can see that the Border gets the DeepPurple color defined in the App.xaml file. So that connection seem to work.
When I try to run my application there is an error:
Index was out of range. Must be non-negative and less than the size of the collection.
VS point the error to the beginning of the x:Key parameter of the StaticResourceExtension element in the App.xaml-file.
Is there a solution for this?
I don't need to use a StaticResourceExtension. Just something so I can have an alias.
PrimaryHueLightBrush is the "alias".
What you can do is to create a copy of the brush and reference the copy instead:
<SolidColorBrush x:Key="MyAlias" Color="{Binding Color,
Source={StaticResource PrimaryHueLightBrush}}"/>
I can set vlc options in an app by adding the following to the application.xaml
<Application x:Class="Application"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:ADSNet35"
StartupUri="Start.xaml"
xmlns:sys="clr-namespace:System;assembly=mscorlib">
<Application.Resources>
<x:Array Type="{x:Type sys:String}" x:Key="VlcOptions">
<sys:String>-I</sys:String>
<sys:String>--dummy-quiet</sys:String>
<sys:String>--ignore-config</sys:String>
<sys:String>--no-video-title</sys:String>
<sys:String>--no-sub-autodetect-file</sys:String>
</x:Array>
</Application.Resources>
</Application>
and in the xaml
<wpf:VlcPlayer xmlns:wpf="clr-namespace:Meta.Vlc.Wpf;assembly=Meta.Vlc.Wpf" x:Name="VlcPlayer1" VlcOption="{StaticResource VlcOptions}" />
and everything works great.
I have created a class library usercontrol and have used the following
UserControl x:Class="ucContainer"
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:sys="clr-namespace:System;assembly=mscorlib"
mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="300">
<UserControl.Resources>
<ResourceDictionary>
<x:Array x:Key="VlcOptions" Type="{x:Type sys:String}">
<sys:String>-I</sys:String>
<sys:String>--dummy-quiet</sys:String>
<sys:String>--ignore-config</sys:String>
<sys:String>--no-video-title</sys:String>
<sys:String>--no-sub-autodetect-file</sys:String>
</x:Array>
</ResourceDictionary>
</UserControl.Resources>
<Grid>
<wpf:VlcPlayer xmlns:wpf="clr-namespace:Meta.Vlc.Wpf;assembly=Meta.Vlc.Wpf" x:Name="VlcPlayer1" VlcOption="{StaticResource VlcOptions}" />
</Grid>
When the usercontrol is created I get the following error:
System.Windows.Markup.XamlParseException: 'Cannot convert the value in attribute 'VlcOption' to object of type 'System.String[]'. 'System.Windows.Markup.ArrayExtension' is not a valid value for property 'VlcOption'.
This is my first class library / usercontrol and hope someone can explain what I'm doing wrong.
I took out the <ResourceDictionary> tag and it worked fine.
I am binding a dataset to datagrid in XAML using Object dataprovider.
Here is my code:
<UserControl xmlns:my="clr-namespace:Pauspan_n_"
x:Class="UCitems"
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="700" d:DesignWidth="1100">
<UserControl.Resources>
<ObjectDataProvider x:Key="sizes" ObjectType="{x:Type my:Item_sizes}">
</ObjectDataProvider>
<ObjectDataProvider x:Key="sizes2" ObjectInstance="{StaticResource sizes}" MethodName="Getsizes">
</ObjectDataProvider>
</UserControl.Resources>
<Grid Height="700" Width="1100">
<DataGrid AutoGenerateColumns="False" Height="154" HorizontalAlignment="Left" Margin="819,55,0,0"
Name="dgv_itemsizes" VerticalAlignment="Top" Width="242"
ItemsSource="{Binding Source={StaticResource sizes2}}" ></DataGrid>
</Grid>
</UserControl>
My dataset name is Item_sizes and the datatable name is sizes,
but datagrid shows nothing in it.
I think I may want something like this:
Public Overridable Overloads Function GetSizes() As Item_sizes.sizesDataTable
Me.Adapter.SelectCommand = Me.CommandCollection(0)
Dim dataTable As Item_sizes.sizesDataTable = New Item_sizes.sizesDataTable()
Me.Adapter.Fill(dataTable)
Return dataTable
End Function
I have a custom control that I want to apply some styles from a resource dictionary in another project. For some reason, adding resources in the UserControl.Resources fails, however placing it in the Grid.Resources succeeds;
<UserControl x:Class="My.Name.Space.MyUserControl"
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="200" d:DesignWidth="300">
<UserControl.Resources>
<ResourceDictionary x:Key="UserControlResources">
<!--Putting it here fails-->
</ResourceDictionary>
</UserControl.Resources>
<Grid>
<Grid.Resources>
<ResourceDictionary x:Key="GridResources">
<!--Putting it here succeeds-->
</ResourceDictionary>
</Grid.Resources>
<!--Control XAML-->
</Grid>
</UserControl>
I get an error "The name 'IntializeComponent' does not exist in the current context". However, looking at the autogenerated MyUserControl.g.i.cs file, hey presto; there it is. What am I doing wrong? What is the difference between these two declarations?
Thanks, Sterren
I'm getting a very unusual error on a project that use to work where I'm trying to bind an ENUM to a combo box. To ensure I've not made coding errors, I've made a new usercontrol using SO Question 58743 and ageektrapped as samples for a self contained user control. I'm using .Net4 Client Framework as the environment and VS2010. The xaml is -
<UserControl x:Class="Barcode.Views.UserControl1"
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:System="clr-namespace:System;assembly=mscorlib"
mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="300">
<UserControl.Resources>
<ObjectDataProvider MethodName="GetValues"
ObjectType="{x:Type System:Enum}"
x:Key="AlignmentValues">
<ObjectDataProvider.MethodParameters>
<x:Type TypeName="HorizontalAlignment" />
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
</UserControl.Resources>
<Grid>
<ComboBox Name="myComboBox" SelectedIndex="0" Margin="8"
ItemsSource="{Binding Source={StaticResource AlignmentValues}}" />
</Grid>
</UserControl>
The error that I'm getting on the ComboBox is Error 144 Unable to cast object of type 'System.String' to type 'System.Windows.DataTemplate' after building the project.
I'm at a loss as to what could be causing this error.
Looks like you may be missing the namespace on your HorizontalAlignment. Add a relevant namespace where the HorizontalAlignment type resides.
xmlns:local="clr-namespace:Barcode.Views"
Then modify your XAML to make use of the newly defined namespace...
...
<x:Type TypeName="local:HorizontalAlignment"/>
...
EDIT:
With this being the HorizontalAlignment enum type from within the framework then your code should work as is. I tested it to be certain and it indeed works; as I placed an instance of the UserControl on my Window and it worked without a hitch. Set up an empty project and start from scratch to see if the problem still exists as you may have other factors causing the issue.