如何将复选框的文本绑定到扩展器的标头

时间:2016-01-20 09:27:47

标签: c# wpf xaml data-binding

我有一个包含3个复选框的扩展器。

<Expander Header="Bind Here" Grid.Row="3" Grid.ColumnSpan="2">
     <StackPanel >
          <CheckBox>Test 1</CheckBox>
          <CheckBox>Test 2</CheckBox>
          <CheckBox>Test 3</CheckBox>
     </StackPanel>
</Expander>

我想将已选中复选框的文本附加到扩展器的标头。

是否可以使用绑定?

3 个答案:

答案 0 :(得分:1)

感谢您的回答,但这就是我所做的:

<Expander Header="{Binding SelectedTitle}" Grid.Row="3" Grid.ColumnSpan="2">
 <StackPanel >
      <CheckBox IsChecked="{Binding Path=SelectedItem.isTest1, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">Test 1</CheckBox>
      <CheckBox IsChecked="{Binding Path=SelectedItem.isTest2, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">Test 2</CheckBox>
      <CheckBox IsChecked="{Binding Path=SelectedItem.isTest3, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">Test 3</CheckBox>
 </StackPanel>

 public String SelectedTitle
    {
        get
        {
            StringBuilder builder = new StringBuilder();
                if (SelectedItem.isTest1)
                    builder.Append("Browse subfolders, ");
                if (SelectedItem.isTest2)
                    builder.Append("Enter filename at the panel, ");
                if (SelectedItem.isTest3)
                    builder.Append("Always show scan settings, ");
                if (builder.Length == 0)
                    builder.Append("None");


            return builder.ToString().Trim().TrimEnd(',');
        }
    }

private void SelectedItem_PropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        UpdateProperty("SelectedTitle");
    }

答案 1 :(得分:0)

选中复选框后,您需要跟踪是否已选中,您可以通过绑定或事件处理或触发来执行此操作。

以下是一个简单的例子

使用绑定(仅限XAML)

如果你热衷于将它作为绑定,你可以尝试在扩展器的控制模板上设置触发器和设置器(我将它留给用户编辑并生成一个最小的xaml,以下部分可以工作,但可能不是最小的) - -

<Window.Resources>
    <ControlTemplate x:Key="ControlTemplate" TargetType="Expander">
        <Expander x:Name="MyExpander">
            <StackPanel Orientation="Vertical">
                <CheckBox x:Name="Box1">Test 1</CheckBox>
                <CheckBox x:Name="Box2">Test 2</CheckBox>
                <CheckBox x:Name="Box3"> Test 3</CheckBox>
            </StackPanel>
        </Expander>
        <ControlTemplate.Triggers>
            <Trigger  SourceName="Box1" Property="CheckBox.IsChecked" Value="True">
                <Setter Property="Header"
                        TargetName="MyExpander"
                        Value="{Binding Content, ElementName=Box1 }" />
            </Trigger>
            <Trigger SourceName="Box2"
                     Property="CheckBox.IsChecked"
                     Value="True">
                <Setter Property="Header" TargetName="MyExpander"
                        Value="{Binding Content, ElementName=Box2 }" />
            </Trigger>
            <Trigger SourceName="Box3"
                     Property="CheckBox.IsChecked"
                     Value="True">
                <Setter Property="Header"
                        TargetName="MyExpander"
                        Value="{Binding Content, ElementName=Box3 }" />
            </Trigger>
        </ControlTemplate.Triggers>
    </ControlTemplate>
</Window.Resources>

<Expander Template="{StaticResource ControlTemplate}"/>

或者如果你不介意代码

代码背后

private void ToggleButton_OnChecked(object sender, RoutedEventArgs e)
    {
        var checkBox = ((CheckBox) sender);

        if (checkBox.IsChecked != null && checkBox.IsChecked.Value)
        {
            MyExpander.Header = checkBox.Content.ToString();
        }
    }

Xaml -

<Expander x:Name="MyExpander">
    <StackPanel>
        <CheckBox Checked="ToggleButton_OnChecked">Test 1</CheckBox>
        <CheckBox  Checked="ToggleButton_OnChecked">Test 2</CheckBox>
        <CheckBox  Checked="ToggleButton_OnChecked">Test 3</CheckBox>
    </StackPanel>
</Expander>

答案 2 :(得分:0)

这是一个很酷的方法:(绑定+转换器)

<强> XAML:

 <Expander Grid.Row="3" Grid.ColumnSpan="2" Name="expander" Width="500">
            <Expander.Resources>
                <local:ExpanderHeaderConverter x:Key="ExpanderHeaderConverter" />
            </Expander.Resources>
            <Expander.Header>
                <TextBlock >
                    <TextBlock.Text>
                        <MultiBinding Converter="{StaticResource ExpanderHeaderConverter}">
                            <Binding ElementName="c1" Mode="OneWay"/>        
                            <Binding ElementName="c1" Mode="OneWay" Path="IsChecked"/>
                            <Binding ElementName="c2" Mode="OneWay"/>         
                            <Binding ElementName="c2" Mode="OneWay" Path="IsChecked"/>
                            <Binding ElementName="c3" Mode="OneWay"/>
                            <Binding ElementName="c3" Mode="OneWay" Path="IsChecked"/>
                        </MultiBinding>
                    </TextBlock.Text>
                </TextBlock>
            </Expander.Header>
            <StackPanel>
                <CheckBox Name="c1" >Test 1</CheckBox>
                <CheckBox Name="c2">Test 2</CheckBox>
                <CheckBox Name="c3">Test 3</CheckBox>
            </StackPanel>
        </Expander>

<强>转换器:

public class ExpanderHeaderConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        string output = string.Empty;
        for (int i = 0; i < values.Length; i+=2)
        {
            if ((values[i + 1] as bool?) == true)
                output += (values[i] as CheckBox).Content.ToString()+" ";
        }
        return output;
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

enter image description here