2016-08-09 12 views
0

いくつかのDependencyPropertyでCustomControlを構築しています。 パスを参照する単純なコントロールで、ラベル、テキストボックス、ボタンが含まれています。GridLengthのDependencyProperty型がCustomControlのChildElementに値 "Auto"を渡していません

ラベルの幅、テキストボックス、ボタンを札幌に設定したいと思います。 私はいくつかの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コードです:文字列型のDependencyPropertyを持つ

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

すべてのバインディングが完璧な作業が、値が「自動」とWidthプロパティの滞在されています。 私はコンバータでテストし、DPに "200"の値を設定しました。 "LabelWidth"は{200}として表示されます(コンバータでブレークポイントがヒットしたときに値がマウスオーバーしたとき)変化する。 私はDoubleGridLengthを変更すると、私はLabelクラスのWidthプロパティのタイプはdoubleあるAuto

答えて

1

最終的に答えが見つかりました。

プロパティWidthの前に属性[TypeConverterAttribute(typeof(LengthConverter))]を追加する必要がありました。私はAutoにLabelWidthを設定することができるよりも

public static DependencyProperty labelWidth = DependencyProperty.Register("LabelWidth", typeof(Double), typeof(PathBrowser), 
    new FrameworkPropertyMetadata(Double.NaN, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault)); 

<local:PathBrower LabelWidth="Auto" /> 
1

にそれを設定することはできません。したがって、LabelWidthのタイプはdoubleで、GridLengthではありません。

+0

そして、どのように私は "オート" に幅を設定することができます

[TypeConverterAttribute(typeof(LengthConverter))] public Double LabelWidth { get { return (Double)GetValue(labelWidth); } set { SetValue(labelWidth, value); } } 

は、その後、私はにDependencyPropertyを変えましたか? –

+0

私を指摘してくれてありがとう。私はそれを知っていましたが、あなたの答えは、MSDNのドキュメントで 'FrameworkElement'の' Width'プロパティについて深く掘り下げて指示しました。そこにanwserが見つかりました。 –

関連する問題