2017-06-06 7 views
0

(ResourceDictionaryの)コードの背後にある名前付きコントロールにアクセスすることは可能ですか?名前付きコントロールへの(ResourceDictionaryの)コードの背後からアクセスできますか?

など。私にとっては、たくさんのフォルダピッキングダイアログを作成する必要があります。ダイアログには、選択する必要があるフォルダごとに複数の行が含まれている場合があります。 各行は、ラベル(名前)、テキストボックス(選択されたパス)、ボタン(FileBrowserDialogを開きます)で構成されます。

これで、FileBrowserDialogの終了時にTextBoxにアクセスします。しかし、私はCodeBehindから "SelectedFolderTextBox"にアクセスすることはできません。

私がしたいことを達成するためのより良い方法はありますか?

XAML

<ResourceDictionary ...> 
    ... 

    <StackPanel x:Key="FolderSearchPanel" 
       x:Shared="False"> 
     <Label Content="Foldername"/> 
     <TextBox x:Name="SelectedFolderTextBox" 
       Text="C:\Folder\Path\"/> 
     <Button Content="..." 
       Click="Button_Click"/> 
    </StackPanel> 
</ResourceDictionary> 

分離コード

private void Button_Click(object sender, RoutedEventArgs e) 
{ 
    // Initialize and show 
    var dialog = new System.Windows.Forms.FolderBrowserDialog(); 
    System.Windows.Forms.DialogResult result = dialog.ShowDialog(); 

    // Process result 
    if (result == System.Windows.Forms.DialogResult.OK) 
    { 
     string selectedPath = dialog.SelectedPath; 

     SelectedFolderTextBox.Text = selectedPath; // THIS DOES NOT WORK 
                // since I don't have access to it 
                // but describes best, what I want to do 
    } 
} 

答えて

0

あなたがコントロールと関連する機能のビットの繰り返しグループを持っている場合、それは、再利用可能なコントロールを作成することは理にかなって:このような何か

プロジェクト「項目の追加」ダイアログでユーザーコントロールを追加し、これを使用しますXAMLとコード:

<UserControl x:Class="WpfDemos.FolderPicker" 
      x:Name="folderPicker" 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      mc:Ignorable="d" 
      d:DesignHeight="75" d:DesignWidth="300"> 
    <StackPanel> 
     <Label Content="{Binding Path=Title, ElementName=folderPicker}"/> 
     <TextBox x:Name="SelectedFolderTextBox" 
       Text="{Binding Path=FullPath, ElementName=folderPicker, 
           UpdateSourceTrigger=PropertyChanged}"/> 
     <Button Content="..." Click="PickClick"/> 
    </StackPanel> 
</UserControl> 
public partial class FolderPicker : UserControl 
{ 
    public FolderPicker() 
    { 
     InitializeComponent(); 
    } 

    public static readonly DependencyProperty TitleProperty = DependencyProperty.Register(
     "Title", typeof (string), typeof (FolderPicker), new PropertyMetadata("Folder")); 

    public string Title 
    { 
     get { return (string) GetValue(TitleProperty); } 
     set { SetValue(TitleProperty, value); } 
    } 

    public static readonly DependencyProperty FullPathProperty = DependencyProperty.Register(
     "FullPath", typeof (string), typeof (FolderPicker), new FrameworkPropertyMetadata(@"C:\", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault)); 

    public string FullPath 
    { 
     get { return (string) GetValue(FullPathProperty); } 
     set { SetValue(FullPathProperty, value); } 
    } 

    private void PickClick(object sender, RoutedEventArgs e) 
    { 
     using (var dialog = new System.Windows.Forms.FolderBrowserDialog()) 
     { 
      if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK) 
       FullPath = dialog.SelectedPath; 
     } 
    } 
} 

テキストボックスには、コードビハインドからアクセス可能です。依存関係のプロパティTitleFullPathは、さまざまな用途のコントロールをカスタマイズし、ビューモデル(リソースとして宣言されたコントロールグループではできないもの)でバインディングを作成できます。例

ビューモデル:

public class MyViewModel 
{ 
    public string Src { get; set; } 
    public string Target { get; set; } 
} 

ビュー:

public MyWindow() 
{ 
    InitializeComponent(); 
    this.DataContext = new MyViewModel { Src = "C:", Target = "D:" } 
} 
<StackPanel> 
    <wpfDemos:FolderPicker Title="Source"  FullPath="{Binding Path=Src}" /> 
    <wpfDemos:FolderPicker Title="Destination" FullPath="{Binding Path=Target}"/> 
</StackPanel> 
+0

ありがとう、あなたは私が遭遇した私の次の質問に答えた!だからUserControlは私が次に作成する必要があります:) ResourceDictionaryは単に "愚かな"素敵な探しているもののためです... – BarbecueSIlver

0

あなたはButtonsender引数をキャストすることができ、その後StackPanelButtonParentプロパティをキャストし、見つける必要がありますのコレクションChildrenのコントロール。

private void Button_Click(object sender, RoutedEventArgs e) 
{ 
    // Initialize and show 
    var dialog = new System.Windows.Forms.FolderBrowserDialog(); 
    System.Windows.Forms.DialogResult result = dialog.ShowDialog(); 

    // Process result 
    if (result == System.Windows.Forms.DialogResult.OK) 
    { 
     string selectedPath = dialog.SelectedPath; 

     Button clickedButton = sender as Button; 
     StackPanel sp = clickedButton.Parent as StackPanel; 
     if (sp != null) 
     { 
      TextBox SelectedFolderTextBox = sp.Children.OfType<TextBox>().FirstOrDefault(x => x.Name == "SelectedFolderTextBox"); 
      if (SelectedFolderTextBox != null) 
       SelectedFolderTextBox.Text = selectedPath; 
     } 
    } 
} 
+0

グレート、それは働きました!ご返信ありがとうございます。 :) – BarbecueSIlver

関連する問題