c# - Unable to set Menu width to 100% in WPF. What's wrong with my code? -
i looking way set menu width 100% in wpf. trying create notepad application, , tried following.
<window x:name="notepad" x:class="notepad.mainwindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" title="untitled - notepad" height="350" width="525" windowstate="maximized" sizetocontent="width"> <grid> <richtextbox x:name="maintextbox" horizontalalignment="stretch" verticalalignment="stretch"> <flowdocument> <paragraph> <inlineuicontainer> <menu height="25" horizontalcontentalignment="stretch" > <menuitem header="_file" /> <menuitem header="_edit" /> <menuitem header="_format" /> <menuitem header="_view" /> <menuitem header="_run" /> <menuitem header="_help" /> </menu> </inlineuicontainer> </paragraph> </flowdocument> </richtextbox> </grid> </window>
and
<window x:name="notepad" x:class="notepad.mainwindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" title="untitled - notepad" height="350" width="525" windowstate="maximized" sizetocontent="width"> <grid> <richtextbox x:name="maintextbox" horizontalalignment="stretch" verticalalignment="stretch"> <flowdocument> <paragraph> <inlineuicontainer> <menu height="25" horizontalalignment="stretch" > <menuitem header="_file" /> <menuitem header="_edit" /> <menuitem header="_format" /> <menuitem header="_view" /> <menuitem header="_run" /> <menuitem header="_help" /> </menu> </inlineuicontainer> </paragraph> </flowdocument> </richtextbox> </grid> </window>
the result in both case same. didn't work. here's screenshot.
http://i.stack.imgur.com/elyzu.png
then found following solution.
<window x:name="notepad" x:class="notepad.mainwindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" title="untitled - notepad" height="350" width="525" windowstate="maximized" sizetocontent="width"> <grid> <richtextbox x:name="maintextbox" horizontalalignment="stretch" verticalalignment="stretch"> <flowdocument> <paragraph> <inlineuicontainer> <menu height="25" width="{binding elementname=maintextbox, path=actualwidth}" > <menuitem header="_file" /> <menuitem header="_edit" /> <menuitem header="_format" /> <menuitem header="_view" /> <menuitem header="_run" /> <menuitem header="_help" /> </menu> </inlineuicontainer> </paragraph> </flowdocument> </richtextbox> </grid> </window>
but result not satisfactory.
http://i.stack.imgur.com/dydp5.png
as can see getting kind of margin menu bar. can tell me wrong code (in both cases)?
before implementing approach, set padding="-5,0"
on richtextbox , give try.
or go second way :
<window x:class="menuwidthstack.mainwindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" title="mainwindow" height="350" width="525"> <grid x:name="container"> <richtextbox x:name="maintextbox" padding="0" margin="0" width="{binding elementname=container, path=actualwidth}"> <flowdocument x:name="fd" pagepadding="{binding pagepadding, mode=twoway}" > <paragraph> <inlineuicontainer> <menu height="25" padding="0" width="{binding elementname=container, path=actualwidth}"> <menuitem header="_file" /> <menuitem header="_edit" /> <menuitem header="_format" /> <menuitem header="_view" /> <menuitem header="_run" /> <menuitem header="_help" /> </menu> </inlineuicontainer> </paragraph> </flowdocument> </richtextbox> </grid>
code behind:
public partial class mainwindow : window, inotifypropertychanged { public mainwindow() { initializecomponent(); this.datacontext = this; } private thickness pagepadding; public thickness pagepadding { { return this.pagepadding; } set { this.pagepadding = value; this.changed("pagepadding"); } } private void changed(string name) { var handlers = this.propertychanged; if (handlers != null) { handlers.invoke(this, new propertychangedeventargs(name)); } } public event propertychangedeventhandler propertychanged; }
for reason pagepadding being overwritten "5,0". use databinding, work properly. databind thickness of 0. work, have two-way databind.
just emphasize, trick:
pagepadding="{binding pagepadding, mode=twoway}" on flowdocument
and of course pagepadding property in code behind.
Comments
Post a Comment