Below you will find pages that utilize the taxonomy term “WPF”
Posts
WPF - Refresh / Update WPF controls
相信大家都知道若要釋放些資源去讓畫面得以更新,若不將運算處理切離主執行緒,我們可能會偷懶用 DoEvents 來做。然而, DoEvents 這個方法的功用只是釋放資源,而釋放出的資源為誰所用,這部分我們無法掌控。因此釋放出的資源可能會被拿去做不相干的處理,造成效能嚴重低落。
在 WinForm 的世界裡,這樣的問題比較好處理,因為有許多現成的方法可以指定特定元件去做更新,像是 Update、Refresh、或是 Invalidate。
在 WPF 的世界裡,我們沒現有的方法可以直接叫用,只能用些小技巧兜出類似的功能。像是將下面這段擴充方法加入…
public static class ExtensionMethods { private static Action EmptyDelegate = delegate() { }; public static void Refresh(this UIElement uiElement) { uiElement.Dispatcher.Invoke(DispatcherPriority.Render, EmptyDelegate); } } 程式在撰寫時就可以像下面這樣直接透過 UI 元件觸發更新。
control.Refresh(); Link Refresh / Update WPF controls
read morePosts
WPF - Binding a ListView to DataTable
要在 WPF 中將 DataTable binding 到 ListView 上,我們主要有幾種做法…
像是把 DataTable 轉型成 IListSource 後,叫用 GetList 方法,並將回傳值塞進 ListView 的 ItemSource 屬性
lvTable.ItemsSource = ((IListSource)dt).GetList(); 或是直接將 DataTable 的 DefaultView 塞進 ListView 的 ItemSource 屬性
lvTable.ItemsSource = dt.DefaultView; XAML 這邊我們只要直接將 DataTable 的 Column 繫上 ListView 內 Grid Column 的 DisplayMemberBinding 就可以了。
以個簡單的例子來看,假設我們要繫結的表單長的像下面這樣:
var dt = new DataTable(); dt.Columns.Add( "Key"); dt.Columns.Add( "Value"); dt.Rows.Add( new object[] { "Key1", "Value1"}); dt.Rows.Add( new object[] { "Key2", "Value2"}); ... lvTable.
read morePosts
WPF - DoEvents in WPF
在做大量的運算處理時,不能避免的有時候會需要做 UI 更新的動作,以給予使用者一些反饋。理想上來說,這時我們應該將處理動作切離主執行緒,在另外一個執行緒去處理,需要反饋時再將更新動作帶回主執行緒。但難免有時會偷懶將處理動作直接放在主執行緒上運行,並利用 DoEvents 去釋放資源,讓 UI 得以更新。
以這樣的情景來說,WinForm 中可直接叫用現成的 DoEvents ,但在 WPF 中則無現成的方法可供叫用,所以我們必須借用 WinForm 的 DoEvents 方法。
只要將 System.Windows.Forms.dll 組件加入參考,接著將命名空間 System.Windows.Forms 加入,再呼叫 Application.DoEvents() 方法即可。
using System.Windows.Forms; ... Application.DoEvents(); ... 或者是自行撰寫像下面這樣的程式來處理也可以。
void DoEvents(){ DispatcherFrame f = new DispatcherFrame(); Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Background, (SendOrPostCallback)delegate(object arg) { DispatcherFrame fr = arg as DispatcherFrame; fr.Continue=True; }, f); Dispatcher.PushFrame(frame); } Link DispatcherFrame Class (System.Windows.Threading) 程湘之間: WPF的UI更新方式 Application.Doevents in WPF
read morePosts
WPF - Auto select ListBoxItem when mouse over
在使用 WPF 的 ListBoxItem,若有要在滑鼠游標經過時自動選取的需求,可以為 ListBoxItem 套用像下面這樣的Style:
<ListBox.Resources> <Style BasedOn="{StaticResource {x:Type ListBoxItem}}" TargetType="{x:Type ListBoxItem}"> <Style.Triggers> <DataTrigger Binding="{Binding IsMouseOver, RelativeSource={RelativeSource Self}}" Value="True"> <Setter Property="IsSelected" Value="True" /> </DataTrigger> </Style.Triggers> </Style> </ListBox.Resources> 這個 Style 只是很簡單的透過 DataTrigger 去偵測 ListBoxItem 的 IsMouseOver屬性值,當屬性值為True時觸發,透過 Setter 將 ListBoxItem 的 IsSelected 屬性值設為True。
Style 套上後,滑鼠游標移動過去,就會自動將 ListBoxItem 給選取起來。
read morePosts
[C#][WPF]WPF程式接收視窗訊息
namespace WpfApplication2 { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); this.SourceInitialized += new EventHandler(MainWindow_SourceInitialized); }
void MainWindow_SourceInitialized(object sender, EventArgs e) { IntPtr hwnd = new WindowInteropHelper(this).Handle; HwndSource.FromHwnd(hwnd).AddHook(new HwndSourceHook(WndProc)); } IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled) { switch (msg) { //... } return IntPtr.Zero; } } }
read morePosts
[WPF]FlowDocument
.csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, “Courier New”, courier, monospace; background-color: #ffffff; /white-space: pre;/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .
read morePosts
[WPF]TextBlock
.csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, “Courier New”, courier, monospace; background-color: #ffffff; /white-space: pre;/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .
read morePosts
[WPF]WrapPanel
.csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, “Courier New”, courier, monospace; background-color: #ffffff; /white-space: pre;/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .
read morePosts
[WPF]Canvas
<pre> <span class="attr">xmlns</span><span class="kwrd">="http://schemas.microsoft.com/winfx/2006/xaml/presentation"</span></pre> <pre class="alt"> <span class="attr">xmlns:x</span><span class="kwrd">="http://schemas.microsoft.com/winfx/2006/xaml"</span></pre> <pre> <span class="attr">Title</span><span class="kwrd">="Window1"</span> <span class="attr">Height</span><span class="kwrd">="300"</span> <span class="attr">Width</span><span class="kwrd">="300"</span><span class="kwrd">></span></pre> <pre class="alt"> <span class="kwrd"><</span><span class="html">Canvas</span><span class="kwrd">></span></pre> <pre> <span class="kwrd"><</span><span class="html">TextBlock</span> <span class="attr">Canvas</span>.<span class="attr">Left</span><span class="kwrd">="20"</span> <span class="attr">Canvas</span>.<span class="attr">Top</span><span class="kwrd">="50"</span><span class="kwrd">></span></pre> <pre class="alt"> ● <span class="attr">&lt;</span>- 學校 (20,50)</pre> <pre> <span class="kwrd"></</span><span class="html">TextBlock</span><span class="kwrd">></span></pre> <pre class="alt"> <span class="kwrd"><</span><span class="html">TextBlock</span> <span class="attr">Canvas</span>.<span class="attr">Left</span><span class="kwrd">="150"</span> <span class="attr">Canvas</span>.<span class="attr">Top</span><span class="kwrd">="200"</span><span class="kwrd">></span></pre> <pre> ● <span class="attr">&lt;</span>- 火車站 (150,200)</pre> <pre class="alt"> <span class="kwrd"></</span><span class="html">TextBlock</span><span class="kwrd">></span></pre> <pre> <span class="kwrd"><</span><span class="html">TextBlock</span> <span class="attr">Canvas</span>.
read morePosts
[WPF]Grid
<pre> <span class="attr">xmlns</span><span class="kwrd">="http://schemas.microsoft.com/winfx/2006/xaml/presentation"</span></pre> <pre class="alt"> <span class="attr">xmlns:x</span><span class="kwrd">="http://schemas.microsoft.com/winfx/2006/xaml"</span></pre> <pre> <span class="attr">Title</span><span class="kwrd">="Window1"</span> <span class="attr">Height</span><span class="kwrd">="300"</span> <span class="attr">Width</span><span class="kwrd">="300"</span><span class="kwrd">></span></pre> <pre class="alt"> <span class="kwrd"><</span><span class="html">Grid</span><span class="kwrd">></span></pre> <pre> <span class="kwrd"><</span><span class="html">Grid.RowDefinitions</span><span class="kwrd">></span></pre> <pre class="alt"> <span class="kwrd"><</span><span class="html">RowDefinition</span><span class="kwrd">/></span></pre> <pre> <span class="kwrd"><</span><span class="html">RowDefinition</span><span class="kwrd">/></span></pre> <pre class="alt"> <span class="kwrd"><</span><span class="html">RowDefinition</span><span class="kwrd">/></span></pre> <pre> <span class="kwrd"></</span><span class="html">Grid.RowDefinitions</span><span class="kwrd">></span></pre> <pre class="alt"> <span class="kwrd"><</span><span class="html">Grid.ColumnDefinitions</span> <span class="kwrd">></span></pre> <pre> <span class="kwrd"><</span><span class="html">ColumnDefinition</span><span class="kwrd">/></span></pre> <pre class="alt"> <span class="kwrd"><</span><span class="html">ColumnDefinition</span><span class="kwrd">/></span></pre> <pre> <span class="kwrd"><</span><span class="html">ColumnDefinition</span><span class="kwrd">/></span></pre> <pre class="alt"> <span class="kwrd"></</span><span class="html">Grid.
read morePosts
[WPF]DockPanel
<pre> <span class="attr">xmlns</span><span class="kwrd">="http://schemas.microsoft.com/winfx/2006/xaml/presentation"</span></pre> <pre class="alt"> <span class="attr">xmlns:x</span><span class="kwrd">="http://schemas.microsoft.com/winfx/2006/xaml"</span></pre> <pre> <span class="attr">Title</span><span class="kwrd">="Window1"</span> <span class="attr">Height</span><span class="kwrd">="300"</span> <span class="attr">Width</span><span class="kwrd">="300"</span><span class="kwrd">></span></pre> <pre class="alt"> <span class="kwrd"><</span><span class="html">DockPanel</span> <span class="attr">Height</span><span class="kwrd">="263"</span> <span class="attr">Name</span><span class="kwrd">="DockPanel1"</span> <span class="attr">Width</span><span class="kwrd">="276"</span><span class="kwrd">></span></pre> <pre> <span class="kwrd"><</span><span class="html">Menu</span> <span class="attr">DockPanel</span>.<span class="attr">Dock</span><span class="kwrd">="Top"</span> <span class="kwrd">></span></pre> <pre class="alt"> <span class="kwrd"><</span><span class="html">MenuItem</span> <span class="attr">Header</span><span class="kwrd">="File"</span><span class="kwrd">></span></pre> <pre> <span class="kwrd"><</span><span class="html">MenuItem</span> <span class="attr">Header</span><span class="kwrd">="Open"</span><span class="kwrd">></</span><span class="html">MenuItem</span><span class="kwrd">></span></pre> <pre class="alt"> <span class="kwrd"></</span><span class="html">MenuItem</span><span class="kwrd">></span></pre> <pre> <span class="kwrd"></</span><span class="html">Menu</span><span class="kwrd">></span></pre> <pre class="alt"> </pre> <pre> <span class="kwrd"><</span><span class="html">ToolBarPanel</span> <span class="attr">DockPanel</span>.
read morePosts
[WPF]StackPanel
.csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, “Courier New”, courier, monospace; background-color: #ffffff; /white-space: pre;/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .
read more