使用ElementName的WPF绑定不会更新该值

时间:2019-12-17 09:53:40

标签: c# wpf data-binding caliburn.micro

我将Label绑定到用户帐户的ComboBox的SelectedItem。根据您选择的内容,标签会显示该帐户的相应余额。它完美地显示了不同帐户的余额,但是当我检查用户输入的金额未超过所选帐户的余额时,余额永远不会改变。假设Account1的余额为1500,而Account2的余额为4000。选择Account1在标签中显示1500,选择Account2在标签中显示4000。

但是无论我选择哪个帐户,ViewModel中的Balance属性都保持4000。

我使用Caliburn.Micro来帮助处理MVVM。

我的标签:

@echo off
rem // Change to target directory in advance (of course `pushd`/`popd` could also be used):
cd /D "%UserProfile%\Downloads"
rem // Use a single loop and gather both file name and file date:
for /F "delims= eol=|" %%a in ('dir /B /A:-D /O:-D "*.*"') do (
    set "newest=%%a"
    set "FileDate=%%~ta"
    goto :CONTINUE
)
:CONTINUE
echo Found file: "%newest%" (last mod.: %FileDate%)
pause

我的组合框

<Label x:Name="Balance"
                   Content="{Binding ElementName=Sender, Path=SelectedItem.Balance, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"
                   DockPanel.Dock="Right"
                   HorizontalContentAlignment="Right"
                   Language="da-DK"
                   ContentStringFormat="{}{0:C}"
                   Style="{StaticResource InputBoxPayments}"/>

我的ViewModel

<xctk:WatermarkComboBox x:Name="Sender"
                                    ItemsSource="{Binding Sender, UpdateSourceTrigger=PropertyChanged}"
                                    SelectedItem="{Binding SelectedAccountNmb, Mode=OneTime, UpdateSourceTrigger=PropertyChanged}"
                                    SelectedValue="{Binding Path=SelectedAccountNmb, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                                    SelectedValuePath="AccountNmb"
                                    DisplayMemberPath="AccountName"
                                    DockPanel.Dock="Right"
                                    Watermark="Vælg din konto..."
                                    Style="{StaticResource InputBoxPayments}"/>

1 个答案:

答案 0 :(得分:0)

在组合框中,123SelectedItem,它是一个字符串属性;并且在您标签的内容中,您将绑定到SelectedAccountNmb且没有SelectedItem的组合框string

您可以这样做:

Balance

您不需要在组合框中绑定 public AccountNmb SelectedAccountNmb { get { return _selectedAccountNmb; } set { _selectedAccountNmb = value; NotifyOfPropertyChange(() => SelectedAccountNmb); NotifyOfPropertyChange(() => CanMakePayment); } } ,因为您可以从SelectedValue属性中获取价值