2

目前我有一组切换按钮。我想根据选中的按钮显示我的 TabControl 的不同选项卡。基本上与选择不同选项卡时的行为相同。不确定我的需求是否是无稽之谈,但无论如何。我希望 SelectedTab 根据单击的按钮进行更改。此外,我的 ToggleButtons 是与 Togglebuttons 相结合的 RadioButtons(我只希望一次检查一个)。我想尝试仅在 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

4

2 回答 2

2
  1. You cannot change value of a primitive type declared as resource using xaml. But you can use a property of an object to act as your variable. Eg;

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

    is non-modifiable using XAML. But, Value property of DictionaryEntry (shown below) is modifiable, despite the fact that like int(IntKey), DEKey is non-modifiable too.

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

If I try to change integer(IntKey) via binding , it won't allow. Eg; <TextBox Text="{Binding Mode=OneWay,Source={StaticResource IntKey}}"/> , Mode must be OneWay. TwoWay, OneWayToSource values are not allowed.

But I can write

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

and any textbox value will be updated in Value of DictionaryEntry(DEKey). Note, two-way binding won't work as DictionaryEntry is not a DependencyObject. But you can now change your variable (Value property) the way you like. But only concern is : changes to Value property will not be reflected back in bounded control.

  1. Yes, you can make use of above information to show Tabs w.r.t. radiobuttons with approach given below. For binding to work properly both ways, we need a DependencyObject and a DependencyProperty, so we use FrameworkElement and its Tag property.

This Tag property now mimics your Variable in question.

    <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>

Code-behind

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

Just in case you don't know how to use Blend Behaviors.

A. Include following namespaces :

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

B. Add references to : Microsoft.Expression.Interactions, System.Windows.Interactivity On my system these are found in

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

于 2015-11-10T13:47:29.220 回答
1

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

于 2015-11-09T16:02:36.007 回答