c# - WPF DataGrid - Binding Source and Width to different objects -
in wpf application, have datagrid bound collection in viewmodel, want width of first column equal property on viewmodel itself, so:
public observablecollection<booking> bookings { return repository.bookings(); } public int maxwidth { return 100; }
(i realise there's no point in binding fixed property - it's demo purposes)
<usercontrol> <datagrid datacontext="{binding bookings}"> <datagrid.columns> <datagridtextcolumn width="{binding relativesource={relativesource findancestor, ancestortype={x:type usercontrol}}, path=datacontext.maxwidth}" header="provider" binding="{binding name}" /> </datagrid.columns> </datagrid> </usercontrol>
but if this, width of column remains @ default value - i.e. wide enough accommodate value.
what doing wrong?
edit: attempted this, see happened:
<label name="tricky" content="500" visibility="collapsed"></label> ... <datagridtextcolumn width="{binding elementname=tricky, path=content}" header="provider" binding="{binding name}" />
which did nothing. not possible set width dynamically?
i think problem datagridtextcolum
not part of visual tree (can verify snoop), binding never gets resolved.
the data obtained datagridtextcolumn
used build datagridcell template, , @ time template built, datacontext
isn't set.
my suggestion use datagridtemplatecolumn
, , specify own celltemplate
has width
binding need.
Comments
Post a Comment