TabControl - ToggleButtons 作为 TabSeletors(XAML-仅逻辑)

TabControl - ToggleButtons as TabSeletors (XAML-Only Logic)

目前我有一组切换按钮。 我想根据选中的按钮显示 TabControl 的不同选项卡。基本上与选择不同选项卡时的行为相同。不确定我的需求是否无稽之谈,但无论如何。我希望 SelectedTab 根据单击哪个按钮而改变。此外,我的 ToggleButtons 是 RadioButtons stlyed to 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>

我想的是 (pseudoCode):

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

基本上甚至可以为 XAML 中的变量赋值,如果是 - 如何?

作为一个例子,说明为什么以及我试图实现的目标是这样的 GUI:

无法单独通过 XAML 中的事件触发器更改 StaticResource 的值。这必须通过将您的 StaticResource 绑定到 ViewModel 的 属性 或使用隐藏代码来完成。

  1. 您不能使用 xaml 更改声明为资源的原始类型的值。但是您可以使用对象的 属性 作为您的变量。例如;

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

    不可使用 XAML 修改。但是,DictionaryEntry(如下所示)的值 属性 是可修改的,尽管 DEKey 与 int(IntKey) 一样也是不可修改的。

    <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}}"/> 

并且任何文本框值都将在 DictionaryEntry(DEKey) 的值中更新。请注意,双向绑定将不起作用,因为 DictionaryEntry 不是 DependencyObject。但是您现在可以按照自己喜欢的方式更改变量 (Value 属性)。但唯一担心的是:对 Value 属性 的更改不会反映在有界控件中。

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

此标记 属性 现在模仿您的相关变量。

    <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" }};

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

一个。包括以下命名空间:

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

乙。添加对 : Microsoft.Expression.Interactions、System.Windows.Interactivity 的引用 在我的系统中,这些可以在

中找到

C:\Program Files (x86)\Microsoft SDKs\Expression\Blend.NETFramework\v4.0\Libraries