web.config绑定 - ItemsControl与其项目源

时间:2015-10-14 07:58:55

标签: wpf binding web-config connection-string observablecollection

我正在构建一个工具来显示和编辑web.config,并希望添加一个" ADD"按钮,以便我可以向web.config添加一个额外的连接字符串。

视图模型:

    private ConnectionStringSettingsCollection _ConnectionStrings;
    public  ConnectionStringSettingsCollection ConnectionStrings
    {
        get
        {
            return _ConnectionStrings;
        }
        set
        {
            if (_ConnectionStrings != value)
            {
                _ConnectionStrings = value;
                RaisePropertyChanged("ConnectionStrings");
            }
        }
    }

    private void ExecuteAddConnectionString()
    {
        ConnectionStrings.Add(new ConnectionStringSettings("a","b","c"));

    }

    private void ReadConfig()
    {
        ConnectionStrings = config.ConnectionStrings.ConnectionStrings;
    }

    public void CreateConfig()
    {
        string webDirPath = (PathWithoutFile());
        var vdm = new VirtualDirectoryMapping(webDirPath, true, "web.config");
        var wcfm = new WebConfigurationFileMap();
        wcfm.VirtualDirectories.Add("/", vdm);
        config = WebConfigurationManager.OpenMappedWebConfiguration(wcfm, "/");
    }

查看:

        <TabItem Header="ConnectionString">
                <GroupBox Header="ConnectionStrings" BorderBrush="Black" BorderThickness="2" Margin="5">
                    <ListBox ItemsSource="{Binding ConnectionStrings}" Margin="5" Background="Transparent" BorderThickness="0">
                        <ListBox.ItemTemplate >
                            <DataTemplate >
                                <ContentControl >
                                    <vm:ConnectionStringAdd Width="400" Margin="5"/>
                                </ContentControl>
                            </DataTemplate>
                        </ListBox.ItemTemplate>
                    </ListBox>
                </GroupBox>
        </TabItem>

所以当我点击添加按钮时,它会给我以下错误:

  

ItemsControl与其商品来源

不一致

我知道这是因为GUI没有收到有关更改的通知,但在我的情况下如何解决?我可以以某种方式将Connectionstring转换为可观察的集合吗?

非常感谢

0 个答案:

没有答案