c# - Adding checkBoxes to UniformGrid -
i trying dynamically add checkboxes uniformgrid in wpf. looks grid doesn't allocate them enough space , kinda lay on each other. how add them in code behind:
foreach (string folder in subfolders) { pathcheckbox chk = new pathcheckbox(); chk.content = new directoryinfo(folder).name; chk.fullpath = folder; chk.horizontalalignment = system.windows.horizontalalignment.stretch; chk.verticalalignment = system.windows.verticalalignment.stretch; unfiformgridsubfolders.children.add(chk); }
this how xaml looks (i placed uniformgrid in scrollviewer)
<scrollviewer grid.column="1" grid.rowspan="3"> <uniformgrid x:name="unfiformgridsubfolders" grid.column="1" grid.row="0" grid.rowspan="3" verticalalignment="stretch" horizontalalignment="stretch"/> </scrollviewer>
and how looks in program:
i want every checkbox has enough space, content can read.
do have to add ui elements dynamically? can't predefine checkbox
template , add checkbox.content
instead? if it's possible define observablecollection
contains checkbox.content
like this:
public observablecollection<string> subfoldernames { get; set; }
then define itemscontrol
, bind it's itemssource
collection:
<itemscontrol grid.row="0" x:name="gridsubfolders" itemssource="{binding subfoldernames}" grid.issharedsizescope="true"> <itemscontrol.itemspanel> <itemspaneltemplate> <wrappanel></wrappanel> </itemspaneltemplate> </itemscontrol.itemspanel> <itemscontrol.itemtemplate> <datatemplate> <grid> <grid.columndefinitions> <columndefinition width="auto" sharedsizegroup="50" /> </grid.columndefinitions> <border margin="5" borderthickness="1" borderbrush="black"> <checkbox content="{binding}"/> </border> </grid> </datatemplate> </itemscontrol.itemtemplate> </itemscontrol>
this way, items have same width share size group, because sized auto
, size largest checkbox.content
.
Comments
Post a Comment