Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 其长度自调整至主面板的直线';s ActualWidth属性(数据绑定)_C#_Wpf_Data Binding - Fatal编程技术网

C# 其长度自调整至主面板的直线';s ActualWidth属性(数据绑定)

C# 其长度自调整至主面板的直线';s ActualWidth属性(数据绑定),c#,wpf,data-binding,C#,Wpf,Data Binding,我有一个垂直的StackPanel,我希望它包含行子级,其.X2值绑定到父StackPanel的实际宽度。比如: StackPanel sp = new StackPanel(); sp.Orientation = Orientation.Vertical; (...) Line line = new Line(); line.X1 = 0; line.X2 = <Some binding to sp.ActualWidth>; // line.Y1 = line.Y2 - will

我有一个垂直的StackPanel,我希望它包含行子级,其.X2值绑定到父StackPanel的实际宽度。比如:

StackPanel sp = new StackPanel();
sp.Orientation = Orientation.Vertical;
(...)
Line line = new Line();
line.X1 = 0;
line.X2 = <Some binding to sp.ActualWidth>;
// line.Y1 = line.Y2 - will it set it's value to 0 by default?
line.StrokeThickness = 2;
(...)
sp.Children.Add(line);
StackPanel sp=new StackPanel();
sp.方向=方向垂直;
(...)
行=新行();
line.X1=0;
line.X2=;
//line.Y1=line.Y2-默认情况下是否将其值设置为0?
line.StrokeThickness=2;
(...)
sp.Children.Add(行);

我应该使用INotifyPropertyChanged接口吗?这是最合适的方法吗?

无需更改InotifyProperty

只需创建一个绑定并将其设置为行的X2属性

        StackPanel sp = new StackPanel();
        sp.Orientation = Orientation.Vertical;
        Line line = new Line();
        line.X1 = 0;
        var bind = new Binding()
        {
            Source = sp,
            Path = new PropertyPath("ActualWidth")
        };
        BindingOperations.SetBinding(line, Line.X2Property, bind);
        line.StrokeThickness = 2;
        line.Stroke=Brushes.Black;
        line.Margin=new Thickness(10);
        sp.Children.Add(line);

INotifyPropertyChanged不是必需的

只需创建一个绑定并将其设置为行的X2属性

        StackPanel sp = new StackPanel();
        sp.Orientation = Orientation.Vertical;
        Line line = new Line();
        line.X1 = 0;
        var bind = new Binding()
        {
            Source = sp,
            Path = new PropertyPath("ActualWidth")
        };
        BindingOperations.SetBinding(line, Line.X2Property, bind);
        line.StrokeThickness = 2;
        line.Stroke=Brushes.Black;
        line.Margin=new Thickness(10);
        sp.Children.Add(line);

除了具有bound
X2
属性和
StrokeThickness
为2的行之外,另一种选择是高度为2的矩形。其宽度自动设置为StackPanel宽度

var sp = new StackPanel
{
    Orientation = Orientation.Vertical
};
...
var line = new Rectangle
{
    Height = 2,
    Fill = Brushes.Black
};
sp.Children.Add(line);

除了具有bound
X2
属性和
StrokeThickness
为2的行之外,另一种选择是高度为2的矩形。其宽度自动设置为StackPanel宽度

var sp = new StackPanel
{
    Orientation = Orientation.Vertical
};
...
var line = new Rectangle
{
    Height = 2,
    Fill = Brushes.Black
};
sp.Children.Add(line);