3

我有一个带有 Listview 的 XAML 代码。现在我想用一个按钮更改 CellTemplate 但没有后面的代码。我怎样才能做到这一点?

模板:

<DataTemplate x:Key="URL"  >
  <TextBlock>
    <Hyperlink NavigateUri="{Binding XPath=@URL}">
      <TextBlock Text="{Binding XPath=@URL}"/>
    </Hyperlink>
  </TextBlock>
</DataTemplate>

<DataTemplate x:Key="Text">
  <TextBlock Text="{Binding XPath=@URL}"/>
</DataTemplate>    


<Grid>  
<Grid.Resources>
  <XmlDataProvider x:Key="Data">
    <x:XData>
      <Data xmlns="">
        <Item ID="1" Desc="Google" URL="http://www.google.com" Acceptable="true"/>
        <Item ID="2" Desc="StackOverflow" URL="http://www.stackoverflow.com" Acceptable="true"/>
        <Item ID="3" Desc="4chan" URL="http://www.4chan.org" Acceptable="false"/>
      </Data>
    </x:XData>
  </XmlDataProvider>
</Grid.Resources>
<Grid.ColumnDefinitions>
  <ColumnDefinition Width="100"/>
  <ColumnDefinition Width="300"/>
</Grid.ColumnDefinitions>

这是应该发生魔术并从 URLColumn 设置 CellTemplate 的按钮。单击此按钮时,我希望将 Text 作为 CellTemplate 。

<Button Grid.Column="0" 
  Name="Text"
  Content="Text"/>

Listview 与 GridViewColumn URLColumn。我想改变它的 CellTemplate。

<ListView 
  Grid.Column="1"
  DataContext="{Binding Source={StaticResource Data}, XPath=/Data}"
  ItemsSource="{Binding XPath=Item}">
  <ListView.View>
    <GridView>
      <GridViewColumn Header="ID" DisplayMemberBinding="{Binding XPath=@ID}"/>
      <GridViewColumn Header="Description" DisplayMemberBinding="{Binding XPath=@Desc}"/>
      <GridViewColumn x:Key="URLColumn" Header="URL" CellTemplate="{StaticResource URL}"/>
      <GridViewColumn Header="Acceptable">
        <GridViewColumn.CellTemplate>
          <DataTemplate>
            <CheckBox IsChecked="{Binding XPath=@Acceptable}"/>
          </DataTemplate>
        </GridViewColumn.CellTemplate>
      </GridViewColumn>
    </GridView>
  </ListView.View>
  </ListView>

这可能没有代码吗?如果是这样,怎么做?我已经在互联网上搜索了一整天,但找不到答案。

谢谢你的帮助!

4

1 回答 1

2

首先安装 Expression Blend Interactivity NuGet 包(或Microsoft.Expression.Interactions.dll手动添加对 Expression Blend SDK 的引用):

Install-Package Blend.Interactivity.Wpf

然后使用ChangePropertyAction触发动作:

<Button
    xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
    xmlns:ic="clr-namespace:Microsoft.Expression.Interactivity.Core;assembly=Microsoft.Expression.Interactions" Grid.Column="0" Name="Text" Content="Text">
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="Click">
            <ic:ChangePropertyAction TargetName="URLColumn" PropertyName="CellTemplate" Value="{StaticResource Text}" />
        </i:EventTrigger>
    </i:Interaction.Triggers>
</Button>
于 2012-10-15T15:33:06.843 回答