Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how would I access this WPF XAML resource programmatically?

Tags:

c#

.net

wpf

xaml

how would I access this WPF XAML resource programmatically?

<Grid.Resources>
<Style x:Key="lineDataPointStyle" TargetType="chartingToolkit:LineDataPoint">
                        <Setter Property="Background" Value="DarkGreen"/>
                        <Setter Property="IsTabStop" Value="False"/>
                        <Setter Property="Template" Value="{x:Null}"/>
                    </Style>
</Grid.Resources>

and here's the code where I want to access it from. Note I need to programmatically create the lines:

 // New Assoicated Graph Series
                var lineSeries = new LineSeries();
                lineSeries.ItemsSource = newSeriesCollection;
                lineSeries.IndependentValuePath = "Second";
                lineSeries.DependentValuePath = "Kb";
                lineSeries.Title = kvp.Key;
                lineSeries.DataPointStyle = (Style) this.Resources["lineDataPointStyle"];  // ** DOES NOT WORK
like image 841
Greg Avatar asked Sep 24 '10 01:09

Greg


2 Answers

I am not sure of the path to the Grid you refer to in your xaml; however, given this xaml:

<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:src="clr-namespace:WpfApplication1"
    Title="Test Application - ListView" Height="300" Width="300">
    <Window.Resources>
        <src:OrderStateConverter x:Key="orderStateConverter"/>
        <DataTemplate x:Key="checkbox">
            <CheckBox IsChecked="{Binding XPath=@State, Converter={StaticResource orderStateConverter}}" 
                  Margin="0,1,1,1" >
            </CheckBox>
        </DataTemplate>
        <DataTemplate x:Key="headerButton">
            <Button/>
        </DataTemplate>
    </Window.Resources>
    <StackPanel>
        <ListView Height="Auto" 
                  Name="listView1" 
                  Width="Auto" 
                  ItemsSource="{Binding Source={StaticResource myXmlDatabase},XPath=Item}">
            <ListView.Resources>
                <DataTemplate x:Key="checkbox2">
                    <CheckBox IsChecked="{Binding XPath=@State, Converter={StaticResource orderStateConverter}}" 
                  Margin="0,1,1,1" >
                    </CheckBox>
                </DataTemplate>
            </ListView.Resources>
        </ListView>
    </StackPanel>
</Window>

and the following code will pull the resource from both the Wndow, and the ListView:

    public void SomeMethod() {
        Object res1 = this.Resources["checkbox"];
        Object res2 = this.listView1.Resources["checkbox2"];
        return;
    }

In this case the method is in the window code behind class

like image 177
Steve Ellinger Avatar answered Sep 20 '22 14:09

Steve Ellinger


FrameworkElement class has public object FindResource(object resourceKey); method. Use this method to search resources.

Cause this.Resources["checkbox"] will not give you resource if it is defined is a hierarchy of Resource Dictionaries and App Resources But this.FindResource("checkbox"); will work there too.

like image 24
Kylo Ren Avatar answered Sep 19 '22 14:09

Kylo Ren