相关疑难解决方法(0)

如何将CollectionContainer绑定到视图模型中的集合?

我有一个视图模型,其中包含一个公开事物集合的属性.我有一个ComboBox,其ItemsSource属性绑定到此集合.现在用户可以从列表中进行选择.

我想允许用户清除选择,所以我想向ComboBox添加一个项目(即Null).这很简单.

我决定尝试为ItemsSource使用CompositeCollection,以便我可以将现有列表中的项添加到ComboBox以及额外的Null项.

经过一段时间的斗争,我决定回到CompositeCollection类的文档.我复制了他们的示例并将其修改为使用视图模型而不是静态资源.

我发现当我将CollectionContainer绑定到ViewModel公开的列表时,列表中没有任何项目显示.

我不知道如何解决这个问题,我正在寻找关于这个主题的任何建议.

这是我的XAML代码:

<Window Background="CornflowerBlue" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"   xmlns:c="clr-namespace:TryingWPF"  x:Class="CompositeCollections"  Title="CompositeCollections"  SizeToContent="WidthAndHeight">
<Window.Resources>
    <c:CompositeCollectionVM x:Key="CompositeCollectionVM"/>
    <XmlDataProvider x:Key="GreekHeroesData" XPath="GreekHeroes/Hero">
        <x:XData>
            <GreekHeroes xmlns="">
                <Hero Name="Jason" />
                <Hero Name="Hercules" />
                <Hero Name="Bellerophon" />
                <Hero Name="Theseus" />
                <Hero Name="Odysseus" />
                <Hero Name="Perseus" />
            </GreekHeroes>
        </x:XData>
    </XmlDataProvider>
    <DataTemplate DataType="{x:Type c:GreekGod}">
        <TextBlock Text="{Binding Path=Name}" Foreground="Gold"/>
    </DataTemplate>
    <DataTemplate DataType="Hero">
        <TextBlock Text="{Binding XPath=@Name}" Foreground="Cyan"/>
    </DataTemplate>
</Window.Resources>

<StackPanel DataContext="{StaticResource CompositeCollectionVM}">
    <TextBlock FontSize="18" FontWeight="Bold" Margin="10" HorizontalAlignment="Center" Foreground="WhiteSmoke">Trying Composite Collections</TextBlock>
    <DockPanel>
        <ListBox Name="myListBox" Height="300" Background="#99333333">
            <ListBox.ItemsSource> …
Run Code Online (Sandbox Code Playgroud)

vb.net data-binding wpf xaml

51
推荐指数
2
解决办法
3万
查看次数

WPF - 检测转换器中的设计模式

我有一个转换器,我希望能够在设计模式下将值更改为 Visibility.Collapsed。没错,它忽略了 GetIsInDesignMode。

另外,我通过依赖注入(棱镜)绑定虚拟机

转换器:

public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {

        if (DesignerProperties.GetIsInDesignMode(new DependencyObject()))
            return Visibility.Collapsed;

        if (value != null && value is AllowedSourceCode)
        {
            var allowedSourceCode = (AllowedSourceCode)value;

            if (value == null)
                return Visibility.Visible;
            else if (allowedSourceCode.SupportedSourceCodes.Contains(allowedSourceCode.SelectedSourceCode))
            {
                return Visibility.Collapsed;
            }
            else
            return Visibility.Visible;

        }
        return Visibility.Collapsed;
    }
Run Code Online (Sandbox Code Playgroud)

看法:

        <Canvas Visibility="{Binding SupportedSourceCodes,Converter={StaticResource AllowedSourcesConverter}}" Background="Gray" Opacity="0.9"
            Grid.Row="0" Grid.Column="0" Grid.RowSpan="3" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Panel.ZIndex="5"  >
Run Code Online (Sandbox Code Playgroud)

xaml.cs:

        public ACARSubLedgerUC(ACARSubLedgerVM vm)
    {
        InitializeComponent();
        DataContext = vm;
    }
Run Code Online (Sandbox Code Playgroud)

c# wpf visual-studio-2015

6
推荐指数
1
解决办法
2862
查看次数

标签 统计

wpf ×2

c# ×1

data-binding ×1

vb.net ×1

visual-studio-2015 ×1

xaml ×1