无法在Silverlight视图中更新绑定控件

时间:2015-02-02 16:38:02

标签: c# silverlight silverlight-5.0

XAML代码:

                <TextBlock Text="Country" Foreground="white" TextAlignment="Right" Grid.Row="2" Grid.Column="0"  />
                <TextBox 
                    x:Name="txtCountries"
                    Grid.Row="2"
                    Grid.Column="1"
                    Grid.ColumnSpan="2"
                    HorizontalAlignment="Stretch"
                    Margin="2, 2, 2, 2"
                    Text="{Binding PhysicalDeliveryParameters.Countries, Converter={StaticResource EnumerableToTextConverter}, ConverterParameter='...'}"
                    IsReadOnly="True">
                </TextBox>
                <Button
                    Grid.Row="2"
                    Grid.Column="3"
                    Content="..."
                    HorizontalAlignment="Left"
                    Tag="Countries"
                    Click="ButtonBase_OnClick" />

C#代码:

    private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
    {
        PhysicalDeliveryParametersViewModel pvm = GetViewModel();
        GenericObservableCollection<SelectableItem> items = pvm.Countries; 

        PhysicalDeliveryParametersDlg dlg = new PhysicalDeliveryParametersDlg(items);
        dlg.Closed += (o, args) =>
            {
                BindingExpression binding = txtCountries.GetBindingExpression(TextBox.TextProperty);
                if(null != binding)
                    binding.UpdateSource();
            };

        dlg.ShowDialog();
    }

当我单击按钮时,ButtonBase_OnClick()方法执行:出现一个对话框(PhysicalDeliveryParametersDlg类),我选择了一些值。绑定的数据(PhysicalDeliveryParameters.Countries,它是一个ObservableCollection)被更新,但不是我的TextBox的Text属性...我做错了吗?

PS:我不确定我是否使用最好的方法在Silverlight中创建模态窗口,你能给我一些建议吗?

1 个答案:

答案 0 :(得分:1)

看起来问题是PropertyChanged永远不会在&#34;国家&#34;属性,因此视图不知道它需要更新。 (实际上,在这种情况下,提升&#34; PropertyChanged&#34;可能没有帮助 - 因为对象引用没有改变,我相信运行时会忽略它。)

我只想添加另一个属性&#34; CountriesString&#34;或类似的:

Text="{Binding PhysicalDeliveryParameters.CountriesString}"

在适当的时候更新房产:

dlg.Closed += (o, args) =>
{
    pvm.CountriesString = string.Join(", ", pvm.Countries);
};
相关问题