Problem in define Orientation Property For My UerControl in WPF?
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:
- Created a UserControl (lets call it MyUserControl).
- Created a dependency property Orientation for MyUserControl.
- 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:
- Be more explicit while asking for help. You stated a problem (and that too not clearly) and then didn't even mention the question. :)
- You may want to check if defining a ControlTemplate helps in your scenario.
精彩评论