如何将属性绑定到App.xaml中的applicationresource

本文关键字:App xaml 中的 applicationresource 绑定 属性 | 更新日期: 2023-09-27 18:04:52

我在app. xml .cs中创建了一个cparameters对象,如下所示:

public partial class App : Application
{
   public CParametres myParamObject;
   protected override void OnStartup(StartupEventArgs e)
   {
       base.OnStartup(e);
       myParamObject    =   new CParametres(System.IO.Path.GetDirectoryName(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName) +@"'BingMapsParam.ini");
       if (myParamObject.LoadParams() == false)
       {
           return;   
       }
       Resources.Add("myParamObject", myParamObject);
   }
}
现在,在我的app.xaml中,我添加了一个字典:
<Application x:Class="myGeoloc.App"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         StartupUri="MainWindow.xaml">
<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="MyDictionary.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

还有,这是我的字典:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                xmlns:t="clr-namespace:myGeoloc"
                xmlns:m="clr-namespace:Microsoft.Maps.MapControl.WPF;assembly=Microsoft.Maps.MapControl.WPF">
<t:CParametres x:Key="myParamObject"/>
<Style TargetType="m:Pushpin" x:Key="PushpinStyle_Fournisseur">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="m:Pushpin" >
                <!--   <Image Stretch="Fill" Source="C:'Users'FabioWalter'Documents'Visual Studio 2013'Projects'myGeoloc'myGeoloc'bin'Debug'Pushpins'PushPinStandard.png" />-->
                <Image Stretch="Fill" Source="{Binding Path=strPicturePushpinFournisseur, Source={StaticResource myParamObject}}" />
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Setter Property="Width" Value="64" />
    <Setter Property="Height" Value="64" />
</Style>
</ResourceDictionary>

strPicturePushpinFournisseur是cparameters中的字符串。该字符串包含图片路径。

实际上,图像没有显示,这与我的错误绑定有关。

有谁能帮我吗?有什么想法吗?

如何将属性绑定到App.xaml中的applicationresource

你可以这样做:

将对象放入资源字典中,使其可用于应用程序的其余部分

    <Application x:Class="SomeNameSpace.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:t="clr-namespace:SomeNameSpace.NameSpaceForT"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
        <ResourceDictionary>
                <ResourceDictionary.MergedDictionaries>
                    <ResourceDictionary Source="View/DictionaryResources/MainResourceDictionary.xaml"/>
                </ResourceDictionary.MergedDictionaries>
                <t:CParametres x:Key="myParamObject"/>
            </ResourceDictionary>
        </Application.Resources>
    </Application>

绑定到任何对象的属性(这是一个示例):

<Window x:Class="SomeNameSpace.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:View="clr-namespace:SomeNameSpace.View"
    Title="MainWindow" 
    DataContext="{Binding Path=strPicturePushpinFournisseur, Source={StaticResource myParamObject}}"
    Height="350" Width="525">
...

编辑


您可以在App.xaml.cs文件中创建对象,并以这种方式添加构造器参数:

public partial class App : Application
{

    protected override void OnStartup(StartupEventArgs e)
    {
        base.OnStartup(e);
        Resources.Add("myParamObject", new CParametres ("Param1", "Param2"));
    }
}

并这样使用:

    <TextBlock Text="{Binding Path=strPicturePushpinFournisseur, Source={StaticResource myParamObject}}"></TextBlock>