我将如何访问此WPF XAML资源编程? [英] how would I access this WPF XAML resource programmatically?

查看:119
本文介绍了我将如何访问此WPF XAML资源编程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将如何编程访问此WPF XAML资源?

 < Grid.Resources>
<风格X:关键=lineDataPointStyle的TargetType =chartingToolkit:LineDataPoint>
                        < setter属性=背景VALUE =深绿/>
                        < setter属性=IsTabStopVALUE =FALSE/>
                        < setter属性=模板值={X:空}/>
                    < /样式和GT;
< /Grid.Resources>
 

和这里就是我想从访问的code。请注意,我需要以编程方式创建行:

  //新Assoicated图系列
                VAR供LineSeries =新供LineSeries();
                lineSeries.ItemsSource = newSeriesCollection;
                lineSeries.IndependentValuePath =二;
                lineSeries.DependentValuePath =KB;
                lineSeries.Title = kvp.Key;
                lineSeries.DataPointStyle =(风格)this.Resources [lineDataPointStyle]; // **不工作
 

解决方案

我不知道的路径,你是指在你的XAML网格的;然而,考虑到本XAML:

 <窗​​口x:类=WpfApplication1.Window1
    的xmlns =htt​​p://schemas.microsoft.com/winfx/2006/xaml/$p$psentation
    的xmlns:X =htt​​p://schemas.microsoft.com/winfx/2006/xaml
    的xmlns:SRC =CLR的命名空间:WpfApplication1
    标题=测试中的应用 -  ListView的高度=300宽度=300>
    < Window.Resources>
        < SRC:OrderStateConverter X:关键=orderStateConverter/>
        < D​​ataTemplate中X:关键=复选框>
            <复选框器isChecked ={结合的XPath = @国,转换器= {的StaticResource orderStateConverter}}
                  保证金=0,1,1,1>
            < /复选框>
        < / DataTemplate中>
        < D​​ataTemplate中X:关键=headerButton>
            <按钮/>
        < / DataTemplate中>
    < /Window.Resources>
    < StackPanel的>
        < ListView的高度=自动
                  NAME =ListView1的
                  WIDTH =自动
                  的ItemsSource ={绑定源= {的StaticResource myXmlDatabase},XPath的=项目}>
            < ListView.Resources>
                < D​​ataTemplate中X:关键=checkbox2>
                    <复选框器isChecked ={结合的XPath = @国,转换器= {的StaticResource orderStateConverter}}
                  保证金=0,1,1,1>
                    < /复选框>
                < / DataTemplate中>
            < /ListView.Resources>
        < / ListView控件>
    < / StackPanel的>
< /窗>
 

和以下code将拉动资源无论从Wndow,并在ListView:

 公共无效的someMethod(){
        对象RES1 = this.Resources [复选框];
        对象RES2 = this.listView1.Resources [checkbox2];
        返回;
    }
 

在这种情况下,该方法是在隐藏类窗口code

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

解决方案

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

这篇关于我将如何访问此WPF XAML资源编程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆