I am returning to WPF after a year of winforms development and am trying to get my eye back in with a small project.
I am creating a simple 'Filename Textbox' which is a usercontrol with a textbox and a button ("...") which spawns the FileOpenDialog. On this usercontrol I have declared a dependency property of Filename
which the Textbox is bound to using a reference to the ElementName of the usercontrol. In the button click handler if the user selects a file in the FileOpenDialog the Filename
property is set.
<TextBox >
<TextBox.Text>
<Binding Path="Filename"
ElementName="FilePickerUC"
UpdateSourceTrigger="PropertyChanged">
<Binding.ValidationRules>
<localvalidator:FilenameValidator />
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>
<Button Click="Button_Click">...</Button>
In my 'main' application I am including this file picker usercontrol and binding its Filename property to a dependency property on the main windows datacontext.
<local:FilePickerControl Filename="{Binding Path=ConfigFilename, Mode=TwoWay}" />
This all works well and good, However as I have a validator on the textbox only when a valid filename is entered does the property get written back to the Main windows datacontext. What I would like is if I can propagate the 'validity' of the filepicker to the main window. How is the best way of going about this? The options I think I have are:
- Include another dependency property on the Filepicker usercontrol called 'valid', then bind this value to the validation rule somehow (how might I do this?)
- Somehow bind from the main window into the usercontrol to extract the validity based on the validation rule - I don't like the close coupling of this idea!
Any suggestions welcomed!