Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/heroku/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 设置不带ItemsControl ItemsSource绑定属性的画布子属性_C#_.net_Wpf_Xaml - Fatal编程技术网

C# 设置不带ItemsControl ItemsSource绑定属性的画布子属性

C# 设置不带ItemsControl ItemsSource绑定属性的画布子属性,c#,.net,wpf,xaml,C#,.net,Wpf,Xaml,有没有办法在没有ItemsControl ItemsSource绑定属性的情况下设置Canvas子属性 为了将我的视图与viewmodel分开,我必须绑定这些项。 我曾以“CodeProject”的设计师身份使用画布 我使用画布进行拖放。当我在画布中手动工作时,它工作得很好。 这意味着我使用添加和删除子项 myCanvas.Children.Add(userControl); myCanvas.Children.Remove(userControl); 但是,如果我在运行时加载usercon

有没有办法在没有ItemsControl ItemsSource绑定属性的情况下设置Canvas子属性

为了将我的视图与viewmodel分开,我必须绑定这些项。 我曾以“CodeProject”的设计师身份使用画布

我使用画布进行拖放。当我在画布中手动工作时,它工作得很好。 这意味着我使用添加和删除子项

myCanvas.Children.Add(userControl);
myCanvas.Children.Remove(userControl);
但是,如果我在运行时加载usercontrols,它们将作为视图加载

<s:Canvas  AllowDrop="True" >

   <ItemsControl Grid.Row="1" ItemsSource="{Binding Path=userControls}">

        <ItemsControl.ItemsPanel>                        
            <ItemsPanelTemplate>
              <s:Canvas Background="Transparent"/>                                
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>

        <ItemsControl.ItemTemplate>
            <DataTemplate>                                
                <s:ControlItem Content="{Binding Path=MyView}"></s:ControlItem >
            </DataTemplate>
        </ItemsControl.ItemTemplate>

        <ItemsControl.ItemContainerStyle>
            <Style>
                <Setter Property="Canvas.Left" Value="{Binding Path=X}" />
                <Setter Property="Canvas.Top" Value="{Binding Path=Y}" />
            </Style>
        </ItemsControl.ItemContainerStyle> 

    </ItemsControl>

  </s:Canvas>


不,没有。(除了手动清除和添加…)

嗯,是的,只是在画布中绘制项目?:)

编辑

我看到你的更新,不知道你在问什么。如果要将项目拖放到画布上,最好从
ItemsSource
添加/删除项目,而不是从画布手动添加/删除项目。仅从
myCanvas
添加/删除它们不会更新
ItemsSource

我建议你看一看。这意味着您将保留现在拥有的
ItemsControl
,但是当您将一个项放到
画布上时,它会将该对象后面的数据项添加到您调用的
userControls
可观察集合中(我不喜欢这个名称,因为它表明数据项包含UI项,但事实并非如此)

<Canvas>
   <TextBlock Text="I'm Child #1" />
   <TextBlock Text="I'm Child #2" Canvas.Top="50" />
</Canvas>
myCanvas.Children.Add(myTextBlock);

foreach(var someControl in SomeControlList)
    myCanvas.Children.Add(someControl);