1. 如何設定連結?
<Grid xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" >
<Border Background="LightGray" CornerRadius="40" Padding="10" Height="140" HorizontalAlignment="Center" VerticalAlignment="center" BorderBrush="Black" BorderThickness="1">
<TextBlock VerticalAlignment="center"><Hyperlink NavigateUri="http://www.zpxp.com/">BrawDraw.Com Online</Hyperlink></TextBlock>
</Border>
</Grid>
(1)注意這句:<Hyperlink NavigateUri="http://www.zpxp.com/">BrawDraw.Com Online</Hyperlink>
2. 如何畫圓角矩形?注意上面(1)Border標記中的CornerRadius="40",它用來指定圓角矩形的圓角半徑
3. 怎樣象HTML中的CSS一樣設定樣式?使用Style - Setter方式:
<Style TargetType="{x:Type Rectangle}">
<Setter Property="Rectangle.RadiusX" Value="10"/>
<Setter Property="Rectangle.RadiusY" Value="10"/>
</Style>TargetType目標類型,Setter進行對Property屬性賦於Value指定的值.
[關於此問題,可以參見我的另一篇文章:<使用WPF建立炫亮按鈕> http://blog.csdn.net/johnsuna/archive/2007/08/07/1729039.aspx 第3點
:使用Application.Resources設定按鈕屬性(類似CSS樣式單)]
4. 排列類似表格或儲存格之類的使用什麼?使用Grid標記,比如排日曆表
5. 如何畫直線?類似:<Rectangle Fill="Black" RadiusX="0" RadiusY="0" Height="1" Margin="0,20,0,0"/>
6. 如何使用底層API進行圖形映像繪製而不是XAML?首先,由於WPF中不象GDI+中有Graphics對象,因此你無法使用Graphics進行繪圖了,取而代之的是:DrawingContext;類似的,GDI+中的OnPaint已被OnRender取代。
其次,UIElement有一個OnRendar方法,它的定義是:
protected virtual void OnRender (
DrawingContext drawingContext
)
但我們不能直接調用OnRender方法,也不能直接建立DrawingContext執行個體,但可以利用 DrawingGroup.Open 和DrawingVisual.RenderOpen。這裡舉兩個例子:(1)自訂繪製Canvas:(2)儲存圖片到檔案。更多詳情見這篇文章:“WPF中,如何使用映像API進行圖形映像繪製而不是XAML? ” 7.
如何在 WPF 中嵌入Flash(ActiveX):
http://blogs.msdn.com/jijia/archive/2007/06/07/wpf-flash-activex.aspx 8.
在WPF下如何擷取顯示螢幕的高度和寬度,就像winform下的System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width(Height)一樣?double h = SystemParameters.PrimaryScreenHeight;
double w = SystemParameters.PrimaryScreenWidth;以下是XAML代碼,它將螢幕寬度綁定到Button的寬度:
<Button FontSize="8" Margin="10, 10, 5, 5" Grid.Column="0" Grid.Row="5"
HorizontalAlignment="Left"
Height="{x:Static SystemParameters.CaptionHeight}"
Width="{x:Static SystemParameters.PrimaryScreenWidth}">
SystemParameters
</Button>
9. 怎樣製作一個帶三角形的且有文字的自訂控制項?就象一樣:
很簡單,你只需要將TextBlock,Polygon用<StackPanel>組合之後放入Button標籤內即可!
<StackPanel>
<Button Height="50" Width="50">
<StackPanel>
<TextBlock>Play</TextBlock>
<Polygon Points="0,0 0,26 17,13" Fill="Black" />
</StackPanel>
</Button>
</StackPanel>
夠簡單的吧?
10. WPF內如何載入.net1.1或2.0的控制項?
你可以這樣載入.net1.1或2.0的控制項到WPF的XAML中:
<Window x:Class="HostingWfInWpf.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
Title="HostingWfInWpf"
>
<Grid>
<WindowsFormsHost>
<wf:MaskedTextBox x:Name="mtbDate" Mask="00/00/0000"/>
</WindowsFormsHost>
</Grid>
</Window>
同理,你也可以載入Forms中你的自訂控制項:
<WindowsFormsHost>
<wf:BrawDrawGraphControl x:Name="brawdrawControl1" Width="500" Height="300" />
</WindowsFormsHost>
所不同的是,你需要在xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms" 將clr-namespace和assembly設定為相應的值。如果是C#代碼呢?將上面的XAML轉成相應的C#代碼即可。別告訴我你不會喲,如果不會,你就需要加深WPF基礎知識的學習。加油!
11. 如何防止WPF控制項的遮蓋而無法解發相關事件?
在WPF中,控制項是可以樹型嵌套的,例如:
<Grid Name ="mainGrid" >
<Canvas Name ="mainCanvas" Background="Green"/>
</Grid>
這時如果mainGrid容器裡有個Button, 因為mainCanvas的遮蓋而無法觸發Click事件(即使mainCanvas背景為透明也不能觸發)。此時,你需要將背景色設為null,這樣就可以穿透mainCanvas容器的遮蓋了。也有一個前提條件,那就是mainCanvas中沒有控制項遮蓋mainGrid的Button。
12. WPF與GDI+中的顏色有區別嗎?
你可以參見我的這篇文章。《GDI+與WPF中的顏色簡析》http://blog.csdn.net/johnsuna/archive/2007/08/27/1761061.aspx
13. WPF中InkCanvas(墨水面板)用法?
參見周銀輝的這篇文章http://www.cnblogs.com/zhouyinhui/archive/2007/08/08/841569.html,贊一下,寫得不錯!
14. 控制WPF圖片的縮放繪圖時的品質?
使用BitmapScalingMode枚舉:
HighQuality:高品質的縮放,比Bilinear縮放方式要慢。
LowQuality:使用Bilinear縮放方式,輸出的映像品質相對較低。
Unspecified:使用預設的縮放方式。
15. WINFORM中如何加入FLASH?又如何在WPF中插入FLASH?
(1)啟動vs2005, 建立一個C#應用程式
(2)在工具箱裡添加一個flash組件,flash.ocx,它是flash的容器,把它拖到Form的內容區,並將之改名為mainFlash
(3)雙擊Form表單加入以下代碼:
string swfFileName = @"test.swf";//這裡你需要根據你的SWF位置進行調整
private void MainForm_Load(object sender, System.EventArgs e)
{
swfFileName = Application.StartupPath + swfFileName; //根據需要進行調整
mainFlash.LoadMovie(0, swfFileName);
} 如果你FLASH中有什麼事件處理(比如點擊按鈕),那麼你可以再找到mainFlash的事件FSCommand,加入下面的代碼:
private void mainFlash_FSCommand(object sender, AxShockwaveFlashObjects._IShockwaveFlashEvents_FSCommandEvent e)
{
MessageBox.Show("Command:" + e.command.ToString() + "/r/n" + "Args:" + e.args .ToString());
}
按F5或Ctrl+F5運行程式,我們會發現FLASH已正常運行。如有上述之點擊按鈕,你可以點擊flash上的按鈕,第一行顯示FLASH的命令,第二行顯示參數。
如何在WPF中插入FLASH?
見這裡: http://blogs.msdn.com/jijia/archive/2007/06/07/wpf-flash-activex.aspx
16. Creating Images from Raw Pixel Data in WPF
double dpi = 96;
int width = 128;
int height = 128; byte[] pixelData = new byte[width*height];
for (int y = 0; y < height; ++y)
{
int yIndex = y * width;
for (int x = 0; x < width; ++x)
{
pixelData[x + yIndex] = (byte) (x + y);
}
}
BitmapSource bmpSource = BitmapSource.Create(width, height, dpi, dpi,
PixelFormats.Gray8, null, pixelData, width);
This creates a BitmapSource of a linear gradient fill from black to white from top left to bottom right.
URL: http://www.interact-sw.co.uk/iangblog/2006/03/10/wpfrawpixels
17.Get Pixel from BitmapSource
private byte[] GetArrayOfPixels(BitmapSource bitmapsource)
{
Int32 stride = bitmapsource.PixelWidth * bitmapsource.Format.BitsPerPixel / 8;
Int32 ByteSize = stride * bitmapsource.PixelHeight * bitmapsource.Format.BitsPerPixel/8;
byte[] arrayofpixel = new byte[ByteSize];
bitmapsource.CopyPixels(arrayofpixel, stride, 0);
return arrayofpixel;
}
private Color ConvertPixelValue(byte[] pixelsvalue, int x, int y)
{
int NumOfBytes = format.BitsPerPixel / 8;
int StartPosition = (y * ImageWidth * (format.BitsPerPixel / 8)) + ( x * (format.BitsPerPixel / 8) );
byte colorvalue = pixelsvalue[StartPosition];
return System.Windows.Media.Color.FromRgb(colorvalue, colorvalue, colorvalue);
//int colorvalue = Convert.ToInt32(pixelsvalue[StartPosition]);
//return System.Drawing.Color.FromArgb(colorvalue,colorvalue,colorvalue);
}
調用:
System.IO.Stream StreamSource = new FileStream (“namafile.tiff”, FileMode.Open, FileAccess.Read, FileShare.Read);
System.Windows.Media.Imaging.TiffBitmapDecoder decoder = new TiffBitmapDecoder (StreamSource, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
BitmapSource bitmapSource = decoder.Frames[0];
byte[] arrayofpixels = GetArrayOfPixels(bitmapSource);
Color c = ConvertPixelValue ( arrayofpixels, 0, 1 );
URL: http://geeks.netindonesia.net/blogs/de_joker/archive/2007/08/28/Get-Pixel-from-BitmapSource.aspx
18. How to convert between WPF BitmapSource objects and a LEAD RasterImage
http://support.leadtools.com/SupportPortal/cs/forums/15388/ShowPost.aspx
19. 使用RichTextBox的技巧:
見:WPF中RichTextBox的使用小竅門(翻譯、整理)
20. 如何旋轉一幅圖片?(NEW!)
其實,超簡單:
XAML代碼:
<Image Width="150" Margin="5">
<Image.Source>
<BitmapImage UriSource="/images/myPhoto.jpg" Rotation="Rotate90" />
</Image.Source>
</Image>
C#代碼:
Image rotated270 = new Image();
rotated270.Width = 150;
BitmapImage bi = new BitmapImage();
bi.BeginInit();
bi.UriSource = new Uri(@"pack://application:,,/images/myPhoto.jpg");
bi.Rotation = Rotation.Rotate270;
bi.EndInit();
rotated270.Source = bi;
更多問題見:
WPF常見問題的問答[2](不斷更新中)