2011-03-03 3 views
2

OpenDialog Windowで選択されているファイル名に基づいて、childWindow内のtextblockを更新する必要があります。私はchildWindowからOpenDialogを実行していないので、その値をChildWindow内のtexblockに渡すのは難しいです。私は誰かが助けることができるかどうか疑問に思います。問題が発生したため、ChildWindow内でOpenDialogを使用できるかどうか疑問に思っていますか?アイデアありがとう!Silverlightでグローバルにtexblock文字列を更新するには?

ChildWindowのXAML:以下

<sdk:ChildWindow 
x:Class="AddPackages_ChildWindow" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk" 
xmlns:toolkit="http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit" 
AutomationProperties.AutomationId="AddPackages_ChildWindow"> 

<Grid x:Name="AddPackages_ChildWindow_LayoutRoot" AutomationProperties.AutomationId="AddPackages_ChildWindow_LayoutRoot" Style="{StaticResource AVV_GridStyle}"> 
    <TextBlock x:Name="txtUpdate_Package" AutomationProperties.AutomationId="txtUpdate_Package" Text="FileName" /> </Grid> 

は、ダイアログボックスを開き、選択したファイル名を渡すためのコードです:

private void Package_Click(object sender, System.Windows.RoutedEventArgs e) 
    { 
     AddPackage_ChildWindow ap = new AddPackage_ChildWindow(); 
     ap.Show(); 

     OpenFileDialog openFileDialog1 = new OpenFileDialog(); 
     openFileDialog1.Filter = "App-V Packages (*.sprj)|*.sprj|App-V Packages (*.sprj)|*.sprj"; 
     openFileDialog1.FilterIndex = 1; 

     openFileDialog1.Multiselect = true; 

     bool? userClickedOK = openFileDialog1.ShowDialog(); 

     if (userClickedOK == true) 
     { 
      //passing the file name string 
      txtUpdate_Package.Text = openFileDialog1.File.Name; 
      System.IO.Stream fileStream = openFileDialog1.File.OpenRead(); 

      using (System.IO.StreamReader reader = new System.IO.StreamReader(fileStream)) 
      { 
       // Read the first line from the file and write it the textbox. 
       // txtUpdate_Package.Text = reader.ReadLine(); 
      } 
      fileStream.Close(); 
     } 
    } 

答えて

1

あなたがそうのようなあなたのChildWindowクラスにSetTextメソッドを公開することができ:

public void SetText(string text) { 
    this.txtUpdate_Package.Text = text; 
} 

次に、あなたのPackage_Clickメソッドからそのようにそれを呼びたい:

ap.SetText(reader.ReadLine()); 
1

あなたがOOの純粋主義者は、あなたのコード内でこの行を変更することができると思うものにすぎ心配していない場合は: -

txtUpdate_Package.Text = openFileDialog1.File.Name; 

ap.txtUpdate_Package.Text = openFileDialog1.File.Name; 

あなたの子ウィンドウ用に作成され、自動生成されたクラスファイルは、XAMLは012種類TextBlockのフィールドと呼ばれていますので、これは動作します - :これまで

内部のアクセス、すなわち

internal TextBlock txUpdate_Package; 

このフィールドは、そのコンストラクタの一部として呼び出さChildWindowのInitializeComponentメソッド中に割り当てられています。

しかし、プライベート内部構造と見なされるべきものに依存するコードを書くのではなく、これを処理するためにパブリックプロパティを作成することをお勧めします。このプロパティを子ウィンドウのコードビハインドに追加します。

public string Text 
{ 
    get { return txtUpdate_Package.Text; } 
    set { txtUpdate_Package.Text = value; } 
} 
関連する問題