2015-11-09 18 views
2

目前我有一组ToggleButtons。 我想根据选中哪个按钮来显示TabControl的一个不同Tab。基本上同样的bahaviour就像选择不同的Tab时一样。不知道我的需求是无稽之谈,但无论如何。我希望SelectedTab根据点击哪个按钮而改变。此外,我的ToggleButtons是RadioButtons,坚持使用Togglebuttons(我只希望一次检查一个)。我想尝试仅在XAML中实现我的需求(如果可能的话)。TabControl - 作为TabSeletors的ToggleButtons(仅限XAML逻辑)

因此,这里是我的XAML的一部分:

<Window.Resources> 
    <sys:Int32 x:Key="CurrentTab"></sys:Int32> 
    <Style TargetType="RadioButton" BasedOn="{StaticResource {x:Type ToggleButton}}"> 
     <Style.Triggers> 
      <Trigger Property="IsChecked" Value="True"> 
       <Setter Property="Background" Value="Aqua"></Setter> 
      </Trigger> 
     </Style.Triggers> 
     <Setter Property="Background" Value="Transparent"></Setter> 
    </Style> 
</Window.Resources> 

    <TabControl Grid.Row="1" 
       Grid.Column="1" 
       Grid.ColumnSpan="2" 
       SelectedIndex="{StaticResource CurrentTab}"> 
     <TabItem Visibility="Collapsed"></TabItem> 
     <TabItem Visibility="Collapsed"></TabItem> 
    </TabControl> 

我在想会是这样的(伪)

<Setter Target="{StaticResource CurrentTab}" Value="{ButtonsToolTip}></Setter> 

基本上是它甚至有可能赋值给变量在XAML中,如果是 - 如何?

至于为什么,什么我尽量做到一个例子是这样的GUI:

http://cache.filehippo.com/img/ex/3049__ccleaner1.png

+0

我不认为这是可能的,而不是在XAML中。您可以将其更改为“DynamicResource”,但仍需要一些C#来更改“CurrentTab”值。 –

+0

@MikeEason嗯没关系。因此,可以说我为我的ButtonStyle添加了一个EventTrigger(CheckChanged或某事),并且在我的代码中的命令后面我将为CurrentTab设置一个值。这是否会自动为我的TabControls SelectedIndex引发propertyChanged事件? –

+0

我仍然不完全确定根据你的描述你想完成什么,但它听起来像你可以设置一个交互触发器在你的切换按钮上执行基于任何ToggleButton的IsChecked的Tab控件上的ChangePropertyAction –

回答

2
  1. 您无法更改使用xaml声明为资源的原始类型的值。但是你可以使用一个对象的属性来充当你的变量。例如;

    <sys:Int32 x:Key="IntKey">12</sys:Int32> 
    

    使用XAML是不可修改的。但是,DictionaryEntry的Value属性(如下所示)是可修改的,尽管类似于int(IntKey),DEKey也是不可修改的。

    <coll:DictionaryEntry x:Key="DEKey" Key="TagKey" Value="100"/> 
    

如果我试图通过结合改变整数(IntKey),它不会允许。例如; <TextBox Text="{Binding Mode=OneWay,Source={StaticResource IntKey}}"/>,模式必须是单向。 TwoWay,OneWayToSource值是不允许的。

但我可以写

<TextBox Text="{Binding Value,Source={StaticResource DEKey}}"/> 

任何文本框的值将在(DEKey)的DictionaryEntry的值进行更新。请注意,双向绑定不起作用,因为DictionaryEntry不是DependencyObject。但是你现在可以按你喜欢的方式改变你的变量(Value属性)。但唯一值得关注的是:Value属性的更改不会反映回有界控制。

  1. 是的,您可以利用上述信息显示标签w.r.t.下面给出的方法的单选按钮。为了使绑定正常工作,我们需要一个DependencyObject和一个DependencyProperty,所​​以我们使用FrameworkElement及其Tag属性。

这个Tag属性现在模仿了您的变量。

<Window.Resources> 
     <FrameworkElement x:Key="rbTagHolder" Tag="0"/> 
    </Window.Resources> 
    ... 
    <ItemsControl x:Name="RadioButtonList"> 
     <ItemsControl.ItemTemplate> 
      <DataTemplate> 
       <RadioButton Content="{Binding TabName}" Tag="{Binding TagValue}" GroupName="Choice"> 
       <i:Interaction.Triggers> 
        <i:EventTrigger EventName="Click"> 
        <ei:ChangePropertyAction TargetObject="{DynamicResource rbTagHolder}" PropertyName="Tag" Value="{Binding Tag, RelativeSource={RelativeSource Mode=FindAncestor, AncestorLevel=1, AncestorType={x:Type RadioButton} }}"/> 
        </i:EventTrigger> 
       </i:Interaction.Triggers> 
       </RadioButton> 
      </DataTemplate> 
     </ItemsControl.ItemTemplate> 
     <ItemsControl.ItemsPanel> 
      <ItemsPanelTemplate> 
       <StackPanel Orientation="Horizontal"/> 
      </ItemsPanelTemplate> 
     </ItemsControl.ItemsPanel> 
    </ItemsControl> 
    ... 
    <TabControl x:Name="TabCtrl" SelectedIndex="{Binding Tag, Source={StaticResource rbTagHolder}}"> ... </TabControl> 

代码隐藏

RadioButtonList.ItemsSource = new[] { new { TabName = "Tab1", TagValue = "0" }, new { TabName = "Tab2", TagValue = "1" }, 
       new { TabName = "Tab3", TagValue = "2" }, new { TabName = "Tab4", TagValue = "3" }}; 

以防万一你不知道如何使用混合行为。

A.包括下列命名空间:

xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions" 
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" 

B.添加引用:Microsoft.Expression.Interactions,System.Windows.Interactivity在我的系统,这些都在

Ç发现:\ Program Files文件(x86)\ Microsoft SDKs \ Expression \ Blend.NETFramework \ v4.0 \ Libraries

+0

好回答坦克很多。 :D –

+0

我刚试过,它的工作就像我想要的一样。如果我在XAML中为每个按钮设置标签,我可以完全不需要代码隐藏文件或ViewModel,就像我想要的一样。我希望你能得到很多积极的选票,因为这个答案很精彩,并且能够满足我100%的需求。非常感谢:P –

+1

这就是我们在这里......帮助别人。 – AnjumSKhan

1

有没有办法在XAML单独事件触发改变StaticResource的价值。这必须通过将您的StaticResource绑定到ViewModel的属性或使用后面的代码来完成。