c# - Binding Style to a TextBox -


i have textbox following:

<textbox   name="txtbackuppath"   grid.column="0"  grid.row="0"  height="auto"   textwrapping="wrap"   style="{binding path=backuppathstyle}"   foreground="{binding path=foregroundcolor}"  text="{binding path=backuppath, mode=twoway}"   verticalalignment="stretch"   acceptsreturn="true"  margin="3,3"   verticalscrollbarvisibility="disabled"  /> 

in code can set foregroundcolor following without errors:

brush _red = brushes.red; backupdirectory.foregroundcolor = _red; 

backupdirectory datasource tied ui. trying use mvvm pattern set ui elements through properties in code. when try use style like:

style style = new style(typeof(textbox)); style.setters.add(new setter(control.foregroundproperty, brushes.goldenrod)); style.setters.add(new setter(control.backgroundproperty, brushes.aqua)); backupdirectory.backuppathstyle = style; 

i "the calling thread cannot access object because different thread owns it." error. don't understand why style , foreground reacts differently when set code.

is possible set style of textbox through property? simplest approach using mvvm?

i "the calling thread cannot access object because different thread owns it." error. don't understand why style , foreground reacts differently when set code.

such properties must set (one , only) ui thread -- code running on background thread. can schedule code executed on ui thread so:

dispatcher.invoke(() => mycontrol.style = new style()); 

that said, including @ least parts of backupdirectory (especially property you're setting) in op helpful.


Comments

Popular posts from this blog

PySide and Qt Properties: Connecting signals from Python to QML -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

scala - 'wrong top statement declaration' when using slick in IntelliJ -