0

我有以下数据模板

        <DataTemplate x:Key="iconButtonsTemplate">
            <StackPanel  Orientation="Horizontal" Margin="120,50,0,0"  HorizontalAlignment="Left" VerticalAlignment="Top"  >
                <icon:IconButton   Command="{Binding Path=DataContext.ButtonClickCommand,RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}"
                                   CommandParameter="{Binding Path=CommandParameter}"    TextContent="{Binding TextContent}" ImageSource="{Binding ImageSource}"
                                   IsIconButtonVisible="{Binding Path=MyLocalBoolList}"  />
            </StackPanel>
        </DataTemplate>

我也有 ItemsControl 这样的,

     <ItemsControl Width="2400"  VerticalAlignment="Top" HorizontalAlignment="Left"
                           ItemsSource="{Binding IconConfigList}"  ItemTemplate="{StaticResource iconButtonsTemplate}" >
                <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>
                        <WrapPanel  Orientation="Horizontal"/>
                    </ItemsPanelTemplate>
                </ItemsControl.ItemsPanel>
      </ItemsControl>

那么是否可以将我的 IsIconButtonVisible 属性绑定到独立于 ItemsControl 中的 IconConfigList 的 ViewModel。

4

2 回答 2

0
IsIconButtonVisible="{Binding Path=DataContext.ViewModelProperty,RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}" 

我希望这将有所帮助。

于 2012-07-23T03:44:29.283 回答
0

在绑定中使用 ElementName 值。

<DataTemplate x:Key="iconButtonsTemplate">
    <StackPanel Orientation="Horizontal" Margin="120,50,0,0"  HorizontalAlignment="Left" VerticalAlignment="Top"  >
        <icon:IconButton Command="{Binding Path=DataContext.ButtonClickCommand,RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}"
                     CommandParameter="{Binding Path=CommandParameter}"    TextContent="{Binding TextContent}" ImageSource="{Binding ImageSource}"
                     IsIconButtonVisible="{Binding Path=DataContext.MyLocalBoolList, ElementName=ItemsControlName}"  />
    </StackPanel>
</DataTemplate>

如果 ElementName 不适合您,请尝试使用 RelativeSource。

于 2012-07-22T23:01:17.003 回答