My question is:
I have this MyAutoCompleteBox
, ACBoxDest
(it inherits from AutoCompleteBox
just with a new event EnterKeyDown
but that should be irrelevant to my question) where the ItemsSource
is just a list of strings set in the codebehind.
I want to bind the text inside ACBoxDest
to always be the same as a public field I have in the codebehind called DestPath
.
Currently, when I change the DestPath
variable in the codebehind, it doesn't also change the ACBoxDest
text in the gui.
The .xaml:
<MyACBox:MyAutoCompleteBox x:Name="ACBoxDest" FilterMode="StartsWith"
Text="{Binding Path=DestPath, Mode=TwoWay}"
ItemContainerStyle="{StaticResource containerStyle}"
Populating="ACBoxDestPopulating"
KeyboardNavigation.TabIndex="1"
EnterKeyDown="EnterKeyHandler"
GotFocus="ACBoxDestFocused"
LostFocus="ACBoxDestUnfocused" />
The codebehind:
public string DestPath {get;set;}
...
private void Initialize()
{
DestPath = _DEFAULT_TARGET_PATH;
if (!ACBoxDest.Text.Equals(DestPath))
MessageBox.Show("ACBoxDest.Text != DestPath");
}
^ For simplicity in explaining debugging, the MessageBox
pops up every time. This means that the text inside ACBoxDest
is not equal to DestPath
, even though they're bound Mode=TwoWay
. Does anybody know why this is happening and how I can fix it?
Much appreciated. Thanks.
Your "DestPath" needs to be a property if you want to use it in Binding.
The source of the binding can be any public property, including properties of other controls, common language runtime (CLR) objects, XAML elements, ADO.NET DataSets, XML Fragments, and so forth.
More details at MSDN
As per comments below: In order to update your UI when property gets updates, your VM class needs to implement INotifyPropertyChanged, and in "set" of DestPath you need to raise property changed event. this MSDN link has details and sample code.