我正在构建一个带有DependencyProperty的CustomControl。 它是浏览路径的简单控件,包含标签,文本框和按钮。
我想设置标签的宽度,文本框和按钮。 所以我为他们添加了一些DependencyProperty。
namespace DDD.AutoRadio.General.UserControls
{
/// <summary>
/// Interaction logic for PathBrowser.xaml
/// </summary>
public partial class PathBrowser : UserControl
{
public PathBrowser() { InitializeComponent(); }
public static DependencyProperty labelContent = DependencyProperty.Register("LabelContent", typeof(string), typeof(PathBrowser));
public static DependencyProperty labelWidth = DependencyProperty.Register("LabelWidth", typeof(GridLength), typeof(PathBrowser),
new FrameworkPropertyMetadata(GridLength.Auto, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));
public static DependencyProperty buttonContent = DependencyProperty.Register("ButtonContent", typeof(string), typeof(PathBrowser),
new FrameworkPropertyMetadata("Blader",FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));
public static DependencyProperty buttonWidth = DependencyProperty.Register("ButtonWidth", typeof(GridLength), typeof(PathBrowser),
new FrameworkPropertyMetadata(default(GridLength), FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));
public static DependencyProperty textBoxText = DependencyProperty.Register("UrlPath", typeof(string), typeof(PathBrowser),
new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,null,null,false,UpdateSourceTrigger.PropertyChanged));
public static DependencyProperty textBoxWidth = DependencyProperty.Register("UrlPathWidth", typeof(GridLength), typeof(PathBrowser),
new FrameworkPropertyMetadata(GridLength.Auto, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));
public static DependencyProperty typeBrowser = DependencyProperty.Register("TypeBrowser", typeof(BrowserType), typeof(PathBrowser),
new FrameworkPropertyMetadata(BrowserType.Directory, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));
public enum BrowserType { Directory,File}
public string LabelContent
{
get { return (string)GetValue(labelContent); }
set { SetValue(labelContent, value); }
}
public GridLength LabelWidth
{
get { return (GridLength)GetValue(labelWidth); }
set { SetValue(labelWidth, value); }
}
public string ButtonContent
{
get { return (string)GetValue(buttonContent); }
set { SetValue(buttonContent, value); }
}
public GridLength ButtonWidth
{
get { return (GridLength)GetValue(buttonWidth); }
set { SetValue(buttonWidth, value); }
}
public string UrlPath
{
get { return (string)GetValue(textBoxText); }
set { SetValue(textBoxText, value); }
}
public GridLength UrlPathWidth
{
get { return (GridLength)GetValue(textBoxWidth); }
set { SetValue(textBoxWidth, value); }
}
public BrowserType TypeBrowser
{
get { return (BrowserType)GetValue(typeBrowser); }
set { SetValue(typeBrowser, value); }
}
private void Button_Click(object sender, RoutedEventArgs e)
{
if (TypeBrowser == BrowserType.Directory)
{
System.Windows.Forms.FolderBrowserDialog op = new System.Windows.Forms.FolderBrowserDialog();
op.SelectedPath = UrlPath;
if (op.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
UrlPath = System.IO.Path.GetFullPath(op.SelectedPath);
}
}
if (TypeBrowser==BrowserType.File)
{
System.Windows.Forms.OpenFileDialog fd = new System.Windows.Forms.OpenFileDialog();
try { fd.InitialDirectory = System.IO.Path.GetDirectoryName(UrlPath); } catch { }
fd.Multiselect = false;
if (fd.ShowDialog()== System.Windows.Forms.DialogResult.OK)
{
UrlPath = System.IO.Path.GetFullPath(fd.FileName);
}
}
}
}
这是我的CustomControl的XML代码:
<UserControl x:Class="DDD.AutoRadio.General.UserControls.PathBrowser"
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"
xmlns:local="clr-namespace:DDD.AutoRadio.General.UserControls"
mc:Ignorable="d" Name="Root"
d:DesignHeight="24" d:DesignWidth="300">
<DockPanel LastChildFill="True">
<Label DockPanel.Dock="Left" Padding="2" Margin="3" Content="{Binding LabelContent,ElementName=Root}"
Width="{Binding LabelWidth,ElementName=Root}"/>
<Button DockPanel.Dock="Right" Padding="2" Margin="3" Content="{Binding ButtonContent,ElementName=Root}"
Width="{Binding ButtonWidth,ElementName=Root}" Click="Button_Click"/>
<TextBox Padding="2" Margin="3" Text="{Binding UrlPath,ElementName=Root}" Width="{Binding UrlPathWidth,ElementName=Root}" />
</DockPanel>
</UserControl>
具有string类型的DependencyProperty的所有绑定都工作正常,但Width属性保持值#34; auto&#34;。
我用转换器进行了测试,值为&#34; 200&#34;设置为DP&#34; LabelWidth&#34;以{200}(当我鼠标悬停&#39;值&#39;当转换器中出现断点时)进入时,标签的宽度不会改变。
当我将GridLength
更改为Double
时,我无法将其设置为Auto
答案 0 :(得分:2)
我终于找到了答案。
我需要在属性Width
之前添加属性[TypeConverterAttribute(typeof(LengthConverter))]
。
[TypeConverterAttribute(typeof(LengthConverter))]
public Double LabelWidth
{
get { return (Double)GetValue(labelWidth); }
set { SetValue(labelWidth, value); }
}
然后我将DependencyProperty
更改为:
public static DependencyProperty labelWidth = DependencyProperty.Register("LabelWidth", typeof(Double), typeof(PathBrowser),
new FrameworkPropertyMetadata(Double.NaN, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));
我可以将LabelWidth设置为Auto
:
<local:PathBrower LabelWidth="Auto" />
答案 1 :(得分:1)
Width
类的Label
属性类型为double
。因此,您的LabelWidth
也应该是double
类型,而不是GridLength
。
答案 2 :(得分:0)
(STL) 我的解决方案
public static readonly DependencyProperty ItemHeightProperty =
DependencyProperty.Register(
"ItemHeight",
typeof (GridLength),
typeof (TreeViewItem),
new FrameworkPropertyMetadata(new GridLength(30)));
/// <summary>
/// Indique si un overlay est présent
/// </summary>
public GridLength ItemHeight
{
get
{
return (GridLength)GetValue(ItemHeightProperty);
}
set
{
SetValue(ItemHeightProperty, value);
}
}