我创建了以下用户控件: - TextBlock:包含要插入的路径的描述 - TextBox:包含目录的名称 - 按钮:允许您在PC中搜索目录并将其放在文本框中
public partial class TextBrowse : System.Windows.Controls.UserControl, INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
public static readonly DependencyProperty SetName =
DependencyProperty.Register("BlockName", typeof(string), typeof(TextBrowse),
new PropertyMetadata(0, new PropertyChangedCallback(
(obj, chng) =>
{
(obj as TextBrowse).BlockName = (string)chng.NewValue;
}
)));
}
public string BlockName
{
get { return (string)GetValue(SetName); }
set { SetValue(SetName, value); }
}
private Type _T;
public Type T
{
set { _T = value; }
get { return _T; }
}
public enum Type { File, Folder }
public TextBrowse()
{
InitializeComponent();
if (System.ComponentModel.DesignerProperties.GetIsInDesignMode(this))
return;
}
private void CButton_Click(object sender, RoutedEventArgs e)
{
if (_T == Type.File)
{
OpenFileDialog ofd = new OpenFileDialog();
if (ofd.ShowDialog() == DialogResult.OK)
this.DirectoryName = ofd.FileName;
}
else
{
FolderBrowserDialog fbd = new FolderBrowserDialog();
if (fbd.ShowDialog() == DialogResult.OK)
CTextbox.Text = fbd.SelectedPath;
}
}
public string DirectoryName
{
get
{
return (string)this.GetValue(DirectoryNameProperty);
}
set
{
this.SetValue(DirectoryNameProperty, value);
}
}
public static readonly DependencyProperty DirectoryNameProperty = DependencyProperty.Register(
"TextName", typeof(string), typeof(TextBrowse), new PropertyMetadata(""));}
这是XAML代码
<UserControl x:Class="XXX.TextBrowse"
x:Name="TB"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<TextBlock Text="{Binding Path=DirectoryName, ElementName=TB, Mode=TwoWay}" Name="CTextBlock" Margin="4" Grid.Column ="0" Grid.Row="0"/>
<Button Name="CButton" Margin="4" Grid.Column = "1" Grid.Row="1" Content="Browse" Click="CButton_Click" />
<TextBox Name="CTextbox" Margin="4" Grid.Column = "0" Grid.Row="1"/>
我想从窗口那样称呼它
xmlns:custom ="clr-namespace:XXX"
Height="300" Width="300">
<Grid>
<custom:TextBrowse BlockName="Name of TextBlock"/>
</Grid>
结果是动态创建用户控件。 TextBlock的名称是在BlockName中输入的值 当通过按钮选择路径时,将在texbox中插入。
正如我所做的那样,我没有工作 最好的关注
答案 0 :(得分:0)
你应该改变这个。
public partial class TextBrowse : UserControl
{
public static readonly DependencyProperty SetName =DependencyProperty.Register("BlockName", typeof(string), typeof(TextBrowse),new PropertyMetadata(null);
public string BlockName
{
get { return (string)GetValue(SetName); }
set { SetValue(SetName, value); }
}
private Type _T;
public Type T
{
set { _T = value; }
get { return _T; }
}
public enum Type { File, Folder }
public TextBrowse()
{
InitializeComponent();
}
private void CButton_Click(object sender, RoutedEventArgs e)
{
if (_T == Type.File)
{
OpenFileDialog ofd = new OpenFileDialog();
if (ofd.ShowDialog() == DialogResult.OK)
this.DirectoryName = ofd.FileName;
}
else
{
FolderBrowserDialog fbd = new FolderBrowserDialog();
if (fbd.ShowDialog() == DialogResult.OK)
CTextbox.Text = fbd.SelectedPath;
}
}
public string DirectoryName
{
get
{
return (string)this.GetValue(DirectoryNameProperty);
}
set
{
this.SetValue(DirectoryNameProperty, value);
}
}
public static readonly DependencyProperty DirectoryNameProperty =DependencyProperty.Register("TextName", typeof(string), typeof(TextBrowse), new PropertyMetadata(""));}