我可以从(ResourceDictionary)后面的代码访问命名控件吗?

时间:2017-06-06 10:16:04

标签: c# wpf controls resourcedictionary xname

是否可以从(ResourceDictionary)后面的代码访问命名控件?

E.g。对我来说,有必要创建大量的文件夹选择对话框。对话框可能包含每个必须选择的文件夹的多行。 每行包括:Label(名称),TextBox(选择路径)和Button(打开FileBrowserDialog)。

所以现在我想在FileBrowserDialog完成后访问TextBox。但我无法访问" SelectedFolderTextBox"来自CodeBehind。

有没有更好的方法来实现我想要做的事情?

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

2 个答案:

答案 0 :(得分:0)

您应该能够将sender参数转换为Button,然后将Parent的{​​{1}}属性转换为Button并找到StackPanel Children集合中的控件。像这样:

StackPanel

答案 1 :(得分:0)

当你有一组重复的控件和一些相关的功能时,创建一个可重用的控件是有意义的:

通过项目“添加项目”对话框添加UserControl并使用此xaml和代码:

string s = "31/1/2016 12:00 AM";
DateTime dt = DateTime.ParseExact(s, "dd/M/yyyy hh:mm tt",CultureInfo.InvariantCulture);
<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>

可以从代码隐藏中访问TextBox。依赖属性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; } } } Title允许自定义对不同用法的控制,并使用视图模型创建绑定(对于声明为资源的控件组,您无法做到这一点)。实施例

查看模型:

FullPath

视图:

public class MyViewModel
{
    public string Src { get; set; }
    public string Target { get; set; }
}
public MyWindow()
{
    InitializeComponent();
    this.DataContext = new MyViewModel { Src = "C:", Target = "D:" }
}