locked
请教一个XMAL 绑定语句 RRS feed

  • 问题

  • 有一个TextBlock和一个ListBox。

    TextBlock的Text绑定ListBox选中项。

    <TextBlock x:Name="txtResult"    Text="{Binding  ElementName=listBox,Path=SelectedItem.Content}" /> 
                                     
                                            <ListBox x:Name="listBox"   >
                                            	<ListBoxItem Content="ListBoxItem1"/>
                                            	<ListBoxItem Content="ListBoxItem2"/>
                                            </ListBox>

    这样可以实现。

    但是如果ListBox使用样式,样式只有一个Button,想让TextBlock的Text绑定这个Button的Content,应该如何改写?

     <ListBox x:Name="listBox"   ItemsSource="{Binding ListBoxSource}"     >
                                            	<ListBox.Resources>
                                            		<Style x:Key="ListBoxItemStyle2" TargetType="ListBoxItem">
                                            			<Setter Property="Background" Value="Transparent"/>
                                            			<Setter Property="BorderThickness" Value="0"/>
                                            			<Setter Property="BorderBrush" Value="Transparent"/>
                                            			<Setter Property="Padding" Value="0"/>
                                            			<Setter Property="HorizontalContentAlignment" Value="Left"/>
                                            			<Setter Property="VerticalContentAlignment" Value="Top"/>
                                            			<Setter Property="Template">
                                            				<Setter.Value>
                                            					<ControlTemplate TargetType="ListBoxItem">
                                            						<Button Content="{Binding butcontent}"/>
                                            					</ControlTemplate>
                                            				</Setter.Value>
                                            			</Setter>
                                            		</Style>
                                            	</ListBox.Resources>
                                            	<ListBox.ItemContainerStyle>
                                            		<StaticResource ResourceKey="ListBoxItemStyle2"/>
                                            	</ListBox.ItemContainerStyle>  
                                            </ListBox>


    不见不散

    2012年3月17日 0:48

答案

  • 你好,

    点击按钮时触发的是Button_Click事件。你可以通过将textblock的Text设为被点击的button的Content来实现目标,例如:

    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="ListBoxItem">
                <Button Height="80" Width="200"  Content="{Binding}" Click="Button_Click"/>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    后台代码:
    private void Button_Click(object sender, RoutedEventArgs e)
            {
                Button btn = sender as Button;
                textBlock1.Text = btn.Content.ToString();
            }

    希望有所帮助。

    • 已标记为答案 Haixia_Xie 2012年3月23日 9:19
    2012年3月19日 3:48
  • 你好,TextBlock和ListBox也在一个样式里,所以textBlock1.Text 不能直接赋值.

    目前我在Button事件里用可视树去找到TextBlock再赋值.


    不见不散

    • 已建议为答案 Haixia_Xie 2012年3月22日 7:29
    • 已标记为答案 Haixia_Xie 2012年3月23日 9:19
    2012年3月20日 9:38

全部回复

  • 点击按钮不能引发ListBox的SelectedChange事件,如何处理?

    不见不散

    2012年3月17日 10:36
  • 你好,

    点击按钮时触发的是Button_Click事件。你可以通过将textblock的Text设为被点击的button的Content来实现目标,例如:

    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="ListBoxItem">
                <Button Height="80" Width="200"  Content="{Binding}" Click="Button_Click"/>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    后台代码:
    private void Button_Click(object sender, RoutedEventArgs e)
            {
                Button btn = sender as Button;
                textBlock1.Text = btn.Content.ToString();
            }

    希望有所帮助。

    • 已标记为答案 Haixia_Xie 2012年3月23日 9:19
    2012年3月19日 3:48
  • 你好,TextBlock和ListBox也在一个样式里,所以textBlock1.Text 不能直接赋值.

    目前我在Button事件里用可视树去找到TextBlock再赋值.


    不见不散

    • 已建议为答案 Haixia_Xie 2012年3月22日 7:29
    • 已标记为答案 Haixia_Xie 2012年3月23日 9:19
    2012年3月20日 9:38