Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/272.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 我可以从(ResourceDictionary的)代码隐藏访问命名控件吗?_C#_Wpf_Controls_Resourcedictionary_Xname - Fatal编程技术网

C# 我可以从(ResourceDictionary的)代码隐藏访问命名控件吗?

C# 我可以从(ResourceDictionary的)代码隐藏访问命名控件吗?,c#,wpf,controls,resourcedictionary,xname,C#,Wpf,Controls,Resourcedictionary,Xname,是否可以从(ResourceDictionary的)代码隐藏访问命名控件 例如,对我来说,有必要创建许多文件夹拾取对话框。对于必须选择的每个文件夹,对话框可能包含多行。 每行包括:标签(名称)、文本框(选定路径)和按钮(打开文件浏览器对话框) 现在我想在FileBrowser对话框完成后访问文本框。但我无法从CodeBehind访问“SelectedFolderTextBox” 有没有更好的方法来实现我想做的事情 XAML <ResourceDictionary ...> .

是否可以从(ResourceDictionary的)代码隐藏访问命名控件

例如,对我来说,有必要创建许多文件夹拾取对话框。对于必须选择的每个文件夹,对话框可能包含多行。 每行包括:标签(名称)、文本框(选定路径)和按钮(打开文件浏览器对话框)

现在我想在FileBrowser对话框完成后访问文本框。但我无法从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;

        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;
        }
    }
}
private void按钮\u单击(对象发送者,路由目标)
{
//初始化并显示
var dialog=new System.Windows.Forms.FolderBrowserDialog();
System.Windows.Forms.DialogResult=dialog.ShowDialog();
//过程结果
if(result==System.Windows.Forms.DialogResult.OK)
{
string selectedPath=dialog.selectedPath;
按钮clickedButton=发送者为按钮;
StackPanel sp=单击按钮。父级为StackPanel;
如果(sp!=null)
{
TextBox SelectedFolderTextBox=sp.Children.OfType().FirstOrDefault(x=>x.Name==“SelectedFolderTextBox”);
如果(SelectedFolderTextBox!=null)
SelectedFolderTextBox.Text=selectedPath;
}
}
}

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

通过项目“添加项”对话框添加UserControl,并使用以下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>
文本框可以从代码隐藏中访问。依赖属性
Title
FullPath
允许为不同的用途自定义控件,并使用视图模型创建绑定(对于声明为资源的控件组,您不能这样做)。范例

视图模型:

public class MyViewModel
{
    public string Src { get; set; }
    public string Target { get; set; }
}
视图:


谢谢,你回答了我遇到的下一个问题!所以UserControl是我接下来需要创建的:)所以ResourceDictionary只是为“愚蠢的”好看的东西准备的。。。
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;
        }
    }
}
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>