c# - WPF InkCanvas - can't change color -
this absolutely ridiculous! i'm trying change color of inkcanvas through code doesn't work. saw lot of tutorials , don't work me. though they're straightforward. i'm new wpf still - should no-brainer.
*note: can set color through xaml that's one-time operation , not want.
my code:
using system.windows; using system.windows.controls; using system.windows.ink; using system.windows.media; namespace wpfapplication1 { public partial class mainwindow : window { inkcanvas inkcanvas = new inkcanvas(); public mainwindow() { initializecomponent(); this.loaded += new routedeventhandler(setcolor); } // doesn't work private void setcolor(object sender, routedeventargs e) { inkcanvas.defaultdrawingattributes.color = colors.red; } // doesn't work either private void button_click(object sender, routedeventargs e) { inkattributes.color = colors.blue; } } }
edit: xaml was:
<window... < inkcanvas name="inkcanvas" /> ....
in code sample, define inkcanvas
in window's code-behind, don't add window's visual controls.
if either specify canvas using xaml:
<window x:class="..." ...> <inkcanvas x:name="inkcanvas"/> </window>
or define in c# , add window:
inkcanvas inkcanvas = new inkcanvas(); public mainwindow() { initializecomponent(); this.loaded += (sender, args) => { this.addchild(inkcanvas); }; }
then line inkcanvas.defaultdrawingattributes.color = colors.red;
shold work.
Comments
Post a Comment