首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >WinUI 3 (0.8.1),C++/WinRT (2.0.210714.1),桌面-使用“绑定操作::集合绑定(...)

WinUI 3 (0.8.1),C++/WinRT (2.0.210714.1),桌面-使用“绑定操作::集合绑定(...)
EN

Stack Overflow用户
提问于 2021-07-20 22:48:08
回答 1查看 166关注 0票数 0

我将Xaml中的一个ListBox绑定到一个NetworkViewModel实例,该实例包含一个NodeViewModel实例的集合。NodeViewModel实例需要在画布上显示在NodeViewModel提供的X和Y位置。

我想将ListBoxItem的Canvas.Left和Canvas.Top绑定到X和Y。我假设在WinUI中不可能在样式中使用绑定。变通方法可以是具有绑定的源路径的附加属性的帮助器类。它在后台代码中的帮助器属性的PropertyChangedCallback中创建绑定。但是绑定不起作用,为什么?

代码语言:javascript
复制
void BindingHelperNodes::OnBindingPathPropertyChanged( winrt::Microsoft::UI::Xaml::DependencyObject const& d,
winrt::Microsoft::UI::Xaml::DependencyPropertyChangedEventArgs const& e )
{
   if(Microsoft::UI::Xaml::Controls::ListBoxItem item{ d.try_as<Microsoft::UI::Xaml::Controls::ListBoxItem>() })
     {
         Microsoft::UI::Xaml::Controls::Canvas::LeftProperty;
        
         Microsoft::UI::Xaml::Data::Binding binding;
         binding.Mode( Microsoft::UI::Xaml::Data::BindingMode::OneWay );
         Microsoft::UI::Xaml::PropertyPath propertyPath( L"Y" );
         binding.Path( propertyPath );
         Microsoft::UI::Xaml::Data::RelativeSource relativeSource;
         relativeSource.Mode( Microsoft::UI::Xaml::Data::RelativeSourceMode::Self );
         binding.RelativeSource( relativeSource );
                
         Microsoft::UI::Xaml::Data::BindingOperations::SetBinding(
             d,
             Microsoft::UI::Xaml::Controls::Canvas::LeftProperty(),
             binding);
         //item.SetBinding( Microsoft::UI::Xaml::Controls::Canvas::LeftProperty(), binding );
     }
 }



 <Grid>
     <Grid.Resources>
         <ResourceDictionary>
             <Style x:Name="listBoxItemContainerStyle2" TargetType="ListBoxItem">
                 <Setter Property="Canvas.Left" Value="0"/>
             </Style>
         </ResourceDictionary>
     </Grid.Resources>
     <ListBox Grid.Row="1" ItemsSource="{x:Bind mainViewModel.NetworkViewModel.Nodes, Mode=OneWay}"
         >
         <ItemsControl.ItemContainerStyle>
             <Style TargetType="ListBoxItem">
                 <Setter Property="local:BindingHelperNodes.CanvasLeftBindingPath" Value="X"/>
             </Style>
         </ItemsControl.ItemContainerStyle>
         <ListBox.Template>
             <ControlTemplate TargetType="ListBox">
                 <ItemsPresenter />
             </ControlTemplate>
         </ListBox.Template>
         <ListBox.ItemsPanel>
             <ItemsPanelTemplate>
                 <Canvas Background="Transparent" />
             </ItemsPanelTemplate>
         </ListBox.ItemsPanel>
         <ListBox.ItemTemplate>
             <DataTemplate x:DataType="local:NodeViewModel">
                 <Grid
                     Width="120"
                     Height="60"
                     >
                     <!-- This rectangle is the main visual for the node. -->
                     <Rectangle
                         Stroke="Black"
                         Fill="White"
                         RadiusX="40"
                         RadiusY="40" 
                     />
             </DataTemplate>
         </ListBox.ItemTemplate>
     </ListBox>
 </Grid>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-07-20 23:43:14

我很幸运地发现了问题所在!真正的问题是对第13行中的SetBinding()的调用,它没有任何效果。但是我实际上忘记了在IDL文件中设置属性"Microsoft.UI.Xaml.Data.Bindable“!

IDL文件:

代码语言:javascript
复制
 namespace NetworkView
 {   
     [Microsoft.UI.Xaml.Data.Bindable]
     runtimeclass NodeViewModel : Microsoft.UI.Xaml.Data.INotifyPropertyChanged
     {
         // Declaring a constructor (or constructors) in the IDL causes the runtime class to be
         // activatable from outside the compilation unit.
         NodeViewModel();
    
         String Name;
         Double X;
         Double Y;
     }
 }

用于创建绑定的CPP文件:

代码语言:javascript
复制
void BindingHelperNodes::OnBindingPathPropertyChanged( winrt::Microsoft::UI::Xaml::DependencyObject const& d, winrt::Microsoft::UI::Xaml::DependencyPropertyChangedEventArgs const& e )
 {
     if(Microsoft::UI::Xaml::Controls::ListBoxItem item{ d.try_as<Microsoft::UI::Xaml::Controls::ListBoxItem>() })
     {
         Microsoft::UI::Xaml::Data::Binding binding;
         binding.Mode( Microsoft::UI::Xaml::Data::BindingMode::OneWay );
         Microsoft::UI::Xaml::PropertyPath propertyPath( L"X" );
         binding.Path( propertyPath );
                
         // Alternative 1 works
         //Microsoft::UI::Xaml::Data::BindingOperations::SetBinding(
         //    d,
         //    Microsoft::UI::Xaml::Controls::Canvas::LeftProperty(),
         //    binding);
         // Alternative 2 works, too
         item.SetBinding( Microsoft::UI::Xaml::Controls::Canvas::LeftProperty(), binding );
     }
 }

向Alfred致以最好的问候

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68456935

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档