开发者

Problem in define Orientation Property For My UerControl in WPF?

开发者 https://www.devze.com 2023-03-13 06:53 出处:网络
i have textbox and progressbar in my usercontrol that place in stackpanel. i write a Orientation Property but it doesnt work???

i have textbox and progressbar in my usercontrol that place in stackpanel. i write a Orientation Property but it doesnt work??? if orientation set to horizontal,textbox 开发者_如何学编程and progressbar should be place horizontally and if orientation set to vertically,textbox and progressbar should be place Vertically. my code is :

<StackPanel Orientation="Horizontal">
        <TextBox Name="TValue" Width="40" Height="23" TextChanged="TValue_TextChanged"/>
        <ProgressBar Name="PG1" Width="200" Height="23"/>
</StackPanel>

and my property is:

//Define Orientation Property

        public Orientation Orientation
        {
            get { return (Orientation)GetValue(OrientationProperty); }
            set { SetValue(OrientationProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Orientation.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty OrientationProperty =
            DependencyProperty.Register("Orientation", typeof(Orientation), typeof(TPUserControl), new UIPropertyMetadata(Orientation.Horizontal));

if u can help me,u save me from storm,THNX


If I understand your problem correctly, you have:

  1. Created a UserControl (lets call it MyUserControl).
  2. Created a dependency property Orientation for MyUserControl.
  3. Within usercontrol you have a stackpanel and you want to bind the Orientation of this stackpanel with Orientation property of MyUserControl.

Try this:

<StackPanel Orientation="{Binding Orientation, 
              RelativeSource={RelativeSource Mode=FindAncestor, 
                                 AncestorType={x:Type MyUserControl }}}">
         <TextBox Name="TValue" Width="40" Height="23" TextChanged="TValue_TextChanged"/>
         <ProgressBar Name="PG1" Width="200" Height="23"/>
</StackPanel> 


Suggestions:

  1. Be more explicit while asking for help. You stated a problem (and that too not clearly) and then didn't even mention the question. :)
  2. You may want to check if defining a ControlTemplate helps in your scenario.
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号