wpf自定義控制元件位置
阿新 • • 發佈:2019-01-26
wpf自定義控制元件的位置與window完全不一樣啊。。。。。。。。。。。真搞騷。
winform自定義控制元件位置通過location 如:Button button =new Button(); button.location=new point(x,y);
而wpf的控制元件自定義位置相對於window來說較為複雜一點。首先將<Grid> 標籤變成<Canvas>
然後通過
Button button2 = new Button();button2.Height = this.Height / 20;
button2.Width = this.Width / 20;
button2.Content = "button2";
button2.SetValue(Canvas.LeftProperty, (double)this.Width * 10 / 16);
button2.SetValue(Canvas.TopProperty, this.Height * 9 / 10); 來實現動態改變button的位置。 wpf為button賦值背景圖片的方式也較為不同。使用下列方式可以實現。 button2.Background = new ImageBrush
{
ImageSource = new BitmapImage(new Uri(Environment.CurrentDirectory+"//button.png"))
}; Environment.CurrentDirectory為程式debug目錄。