Best way to store a row of data relative to items

What's the best, most feasible way proper

to store data inside elements?

I used the dedicated file XML

and now I am using the Tag

and property tooltip

.

This is string data, for example:

Topic data Theme1.fg.ffffffff;Theme2.fg.ff000000;

Margins to fit the window Margin.16:9.10,5,10,5;

+3


source to share


2 answers


In WPF / XAML, the ideal approach would be to store such strings in the Resources

appropriate element or inResourceDictionary

eg,

<Grid x:Name="myGrid" xmlns:sys="clr-namespace:System;assembly=mscorlib">
    <Grid.Resources>
        <sys:String x:Key="ThemeData">Theme1.fg.ffffffff;Theme2.fg.ff000000;</sys:String>
        <sys:String x:Key="Margins">Margin.16:9.10,5,10,5;</sys:String>
    </Grid.Resources>
</Grid>

      

to use the same you have two approaches

xaml approach

<TextBlock Text="{StaticResource ThemeData}" />

      

code for

string themeData = myGrid.FindResource("ThemeData");

      



these resources can also be saved in ResourceDictionary

, which can be combined into any element, window or even an entire application

eg,

StringResources.xaml

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:sys="clr-namespace:System;assembly=mscorlib">
    <sys:String x:Key="ThemeData">Theme1.fg.ffffffff;Theme2.fg.ff000000;</sys:String>
    <sys:String x:Key="Margins">Margin.16:9.10,5,10,5;</sys:String>
</ResourceDictionary>

      

using

<Grid x:Name="myGrid">
    <Grid.Resources>
        <ResourceDictionary Source="StringResources.xaml" />
    </Grid.Resources>
    <TextBlock Text="{StaticResource ThemeData}" />
</Grid>

      

or this if you want to combine / override some more resources

<Grid x:Name="myGrid">
    <Grid.Resources>
        <ResourceDictionary xmlns:sys="clr-namespace:System;assembly=mscorlib">
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="StringResources.xaml" />
            </ResourceDictionary.MergedDictionaries>
            <!--define new resource or even override existing for this specific element -->
            <sys:String x:Key="ThemeData">Theme1.fg.ff00ff00;Theme2.fg.ff0000ff;</sys:String>
            <sys:String x:Key="NewMargins">Margin.16:9.10,5,10,5;</sys:String>
        </ResourceDictionary>
    </Grid.Resources>
    <TextBlock Text="{StaticResource ThemeData}" />
</Grid>

      

0


source


As I understand it, you can use the Tag property on controls to store information. it takes the type of an object. so you can attach any type to it. similar control .Tag = youwantto object attached. if my answer seems inappropriate, please clarify your question



0


source







All Articles