简体   繁体   中英

Single Property bound to multiple controls in WPF

I have a single property that consists of three letters and two numbers, such as "ABC 12". The requirements for my project ask that the UI divide this into a ComboBox for the three letter combinations and a TextBox for the numerical. I can do this pretty easily using a converters to parse out the part of the string that I need.

My question is whether there is a way to implement the "ConvertBack" logic in my converter such that I can reset the property based upon the values in the two different controls.

My xaml:

<ComboBox IsEnabled="{Binding EditMode}" ItemsSource="{Binding AbbrevsList}"      
          DisplayMemberPath="SelectedAbbrev" SelectedValuePath="Abbrev" 
          SelectedValue="{Binding Row.Code, Converter=CodeAlphaConverter, 
                          UpdateSourceTrigger=PropertyChanged}" />
<TextBox TextAlignment="Left">
     <TextBox.Text>
          <Binding Path="Row.Code" Converter="CodeNumericConverter" 
                   UpdateSourceTrigger="LostFocus">
          </Binding>
      </TextBox.Text>
</TextBox>

Thanks.

Since WPF does not allow you to bind converter parameters, you may have to switch your approach. Instead of using converters, other answers on SO point to changing your View Model to have the logic built into two properties and binding to those or, instead of passing in the individual value to the converter, you pass in the entire object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM