2014-05-22 10 views
0

我創建了一個CParametres在我app.xaml.cs這樣的對象:現在如何綁定屬性ApplicationRessource位於App.xaml中

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) [email protected]"\BingMapsParam.ini"); 
     if (myParamObject.LoadParams() == false) 
     { 
      return; 
     } 
     Resources.Add("myParamObject", myParamObject); 
    } 

} 

,在我的App.xaml,我添加Dictionnary:

<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> 

而且,這裏是我的Dictionnary:

<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是CParametres字符串。該字符串包含圖片路徑。

其實,圖像不顯示,它與我的壞綁定有關。

任何人都可以幫助我嗎? 任何想法?

感謝很多:)

回答

0

你可以做一些這樣的事:

,使對象在資源字典使其可應用

<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> 

希望它可以幫助...

+0

感謝,以及如何定義「T:」爲CParametres對象 –

+0

噸是名字空間,如果你正在使用Visual Studio它會自動完成... –

+0

一看便知更新,t的命名空間被添加 –