【问题标题】:Change Template with Button without code behind使用按钮更改模板,无需代码
【发布时间】:2012-10-15 23:33:06
【问题描述】:

我有一个带有 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"/>

带有 GridViewColumn URLColumn 的 Listview。我想把它改成 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>

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

感谢您的帮助!

【问题讨论】:

    标签: wpf xaml


    【解决方案1】:

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

    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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-04-11
      • 2017-02-23
      • 1970-01-01
      • 1970-01-01
      • 2011-03-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多