当前位置:编程学习 > C#/ASP.NET >>

WPF .xaml取值问题

 <!--房态卡片-->
            <DataTemplate x:Key="cardViewDataTemplate" >
            <Border x:Name="card1" Background="Transparent" BorderBrush="Transparent" BorderThickness="1" CornerRadius="10" Margin="10">
                <Grid Margin="0,0,0,0" HorizontalAlignment="Center" VerticalAlignment="Center" ShowGridLines="False" x:Name="card" >
                    <Grid.RowDefinitions>
                        <RowDefinition Height="32"/>
                        <RowDefinition Height="68"/>
                        <RowDefinition Height="20"/>
                        <RowDefinition Height="20"/>
                        <RowDefinition Height="20"/>
                        <RowDefinition Height="20"/>
                        <RowDefinition Height="5"/>
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="100" />
                        <ColumnDefinition Width="*"/>
                    </Grid.ColumnDefinitions>
                    <Border x:Name="cardBorder" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Width="Auto" Height="Auto" Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="2" Grid.RowSpan="7"  Background="{StaticResource cardViewBackgroundBrush}" BorderBrush="LightGray"  BorderThickness="1" Margin="0,1,0,1" CornerRadius="0,0,8,8"/>
                    <Border  x:Name="cardTitle" HorizontalAlignment="Stretch" VerticalAlignment="Top"  Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="2" Width="Auto" Height="32" BorderBrush="{x:Null}" BorderThickness="0,0,0,0"  CornerRadius="0,0,0,0">
                        <Border.Background>
                             <ImageBrush ImageSource="Images\cardTitle.jpg"/>
                        </Border.Background>
                    </Border>
                    
                    <Border HorizontalAlignment="Left" Grid.Row="0" Grid.Column="0" Grid.RowSpan="2" Width="60" Margin="9" Height="60" VerticalAlignment="Top"  >
                        <Border.BitmapEffect>
                            <DropShadowBitmapEffect Direction="316" Color="#FFA9A9A9" ShadowDepth="4" Softness="0.075"/>
                        </Border.BitmapEffect>
                        <Image x:Name="Tm_pic" Width="Auto" Height="Auto" Source="Images\Temperature_Control.gif" Stretch="Fill" StretchDirection="DownOnly" HorizontalAlignment="Stretch" VerticalAlignment="Top" MouseLeftButtonDown="Image_MouseLeftButtonDown"/>
                    </Border>
                        
                         <TextBlock Text="{Binding Path=Room_No}" Grid.Row="0" Grid.Column="1" TextWrapping="Wrap" Margin="2,0,0,0" VerticalAlignment="Center" Height="22" Foreground="Black" FontWeight="Bold" FontSize="16" />
                       <StackPanel Orientation="Horizontal" Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="2" Margin="1">
                             <TextBlock Text="房间温度:" TextWrapping="Wrap" Margin="5,0,0,0" FontSize="12" Foreground="Black"   HorizontalAlignment="Left" VerticalAlignment="Top"/>
                             <TextBlock x:Name="Rm_Tm"  Text="{Binding Path=Room_Temperature}" TextWrapping="Wrap" Margin="1" FontSize="12" Foreground="Black"  VerticalAlignment="Top"/>
                        </StackPanel>
                    
                        <StackPanel Orientation="Horizontal"  Grid.Row="3" Grid.Column="0" Grid.ColumnSpan="2" Margin="1">
                            <TextBlock Text="开关状态: " TextWrapping="Wrap" Margin="5,0,0,0" FontSize="12" Foreground="Black" HorizontalAlignment="Left"   VerticalAlignment="Top"/>
                             <TextBlock  x:Name="ctr_status" Text="{Binding Path=room_ControlStatus}" TextWrapping="Wrap" Margin="1" FontSize="12" Foreground="Black"  VerticalAlignment="Top"/>
                        </StackPanel>
                    
                        <StackPanel Orientation="Horizontal"  Grid.Row="4" Grid.Column="0" Grid.ColumnSpan="2" Margin="1">
                            <TextBlock Text="修改人: " TextWrapping="Wrap" Margin="5,0,0,0" FontSize="12" Foreground="Black" HorizontalAlignment="Left"   VerticalAlignment="Top"/>
                             <TextBlock Text="{Binding Path=sampling_people}" TextWrapping="Wrap" Margin="1" FontSize="12" Foreground="Black"  VerticalAlignment="Top" HorizontalAlignment="Stretch"/>
                        </StackPanel>
                    
                         <StackPanel Orientation="Horizontal"  Grid.Row="5" Grid.Column="0" Grid.ColumnSpan="2" Margin="1">
                            <TextBlock TextWrapping="Wrap" Margin="5,0,0,0" FontSize="12" Foreground="Black" HorizontalAlignment="Left"  VerticalAlignment="Top"/>
                            <TextBlock  Text="{Binding Path=IpAdress}" TextWrapping="Wrap" Margin="1" FontSize="12" Foreground="Black"  VerticalAlignment="Top"/>    
                         </StackPanel>
            </Grid>
            </Border>

</DataTemplate>

-------------------.cs (问题在这里) --------------------
private void Image_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
     {
           //这里如何才能得到我点击的这张图片所对应的TextBlock 值,我点到哪里,就要取出该图标上所有的TextBlock值
              TextBlock b;
            b = this.FindResource("Rm_Tm") as TextBlock;//这样不行
      }
--------------------编程问答-------------------- 为么不把图片做为背景, 直接捕获TextBlock上的鼠标事件? --------------------编程问答-------------------- 你的Tm_pic图片和TextBlock 之间没有任何关系,
怎么可能得到TextBlock 值
要绑定一下,
Rm_Tm是一个TextBlock ,而不是资源,
当然不行了
补充:.NET技术 ,  C#
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,