在初學寫WPF例程的時候 模板列的點擊事件,只能獲取點擊事件,但無法獲取模板列的某一個TextBlock的值
我只是想要在點擊事件的時候獲取某一個值
比如說我有這樣幾條資料
第一個名字:陳真
第二個名字:陳小真
型別:None
第一個名字:諾克薩斯
第二個名字:諾克薩斯之手
型別:Processing
第一個名字:黑色玫瑰
第二個名字:黑色玫瑰風暴
型別:Progress
第一個名字:祖安
第二個名字:祖安狂人
型別:Menaduo
如何在點擊的時候獲取陳真這個值
下面貼出代碼,在線等大佬幫忙... 不勝感激
<Window x:Class="Wpf_Demo_Test.常用控制元件系結以及獲取基本值"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Wpf_Demo_Test"
mc:Ignorable="d"
Title="常用控制元件系結以及獲取基本值" Height="300" Width="300">
<Window.Resources>
<DataTemplate x:Key="listBoxTemplate">
<StackPanel Margin="4">
<DockPanel>
<TextBlock FontWeight="Bold" Text="第一個名字:" DockPanel.Dock="Left" Margin="5,0,10,0"/>
<TextBlock Text=" "/>
<TextBlock Text="{Binding FirstName}" x:Name="FirstName" Foreground="Green" FontWeight="Bold"/>
</DockPanel>
<DockPanel>
<TextBlock FontWeight="Bold" Text="第二個名字:" DockPanel.Dock="Left" Margin="5,0,5,0"/>
<TextBlock Text=" "/>
<TextBlock Text="{Binding LastName}" x:Name="LastName" Foreground="DarkOrange"/>
</DockPanel>
<DockPanel>
<TextBlock FontWeight="Bold" Text="型別:" DockPanel.Dock="Left" Margin="5,0,5,0"/>
<TextBlock Text=" "/>
<TextBlock Text="{Binding Enmu}" x:Name="Enmu" Foreground="Cyan"/>
</DockPanel>
</StackPanel>
</DataTemplate>
</Window.Resources>
<Grid>
<!--系結資料集合中回傳的第一張表-->
<ListBox Margin="17,8,15,26" Name="listBox1" ItemsSource="{Binding Tables[0]}"
ItemTemplate="{StaticResource listBoxTemplate}" SelectionChanged="listBox1_SelectionChanged"/>
</Grid>
</Window>
后臺資料系結,
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
namespace Wpf_Demo_Test
{
/// <summary>
/// 常用控制元件系結以及獲取基本值.xaml 的互動邏輯
/// </summary>
public partial class 常用控制元件系結以及獲取基本值 : Window
{
public 常用控制元件系結以及獲取基本值()
{
InitializeComponent();
BindData();
}
string sql = "select FirstName,LastName,Enmu from Customer";
string connectionString = "Data Source =.; Initial Catalog = customertest; Integrated Security = True";
/// <summary>
/// 頁面加載時系結資料的方法
/// </summary>
private void BindData()
{
DataSet ds = new DataSet();
using (SqlConnection sqlcn = new SqlConnection(connectionString))
{
using (SqlCommand cmd = new SqlCommand(sql, sqlcn))
{
SqlDataAdapter adapter = new SqlDataAdapter();
sqlcn.Open();
adapter.SelectCommand = cmd;
adapter.Fill(ds, "province");
listBox1.DataContext = ds;
}
}
}
//listbox下拉選中事件
private void listBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
//object s1 = listBox1.SelectedValue;
MessageBox.Show("我們選中了這個選項"); //System.Data.DataRowView
//我們不僅是為了顯示物體,我們也要獲取顯示選中項的某一個值
}
}
}
uj5u.com熱心網友回復:
與其用事件, 你還不如用winform去,這樣沒有發揮出WPF的優勢,WFP的MVVM模式很牛逼的,隨便一本介紹WPF的書都會提到,不要用事件,用雙向系結。
<Window x:Class="WPFTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WPFTest"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<ListBox ItemsSource="{Binding People}" SelectedItem="{Binding SelectedPerson}"></ListBox>
</Grid>
</Window>
using System.Windows;
namespace WPFTest
{
/// <summary>
/// MainWindow.xaml 的互動邏輯
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.DataContext = new ViewModel();
}
}
}
using System.Collections.Generic;
using System.Windows;
namespace WPFTest
{
class ViewModel
{
public ViewModel()
{
People = new List<Person> {
new Person { Name1 = "陳真", Name2 = "陳小真" },
new Person { Name1 = "諾克薩斯", Name2 = "諾克薩斯之手" },
new Person { Name1 = "黑色玫瑰", Name2 = "黑色玫瑰風暴" },
};
}
public List<Person> People { get; set; }
private Person _selectedPerson;
public Person SelectedPerson
{
get { return _selectedPerson; }
set
{
_selectedPerson = value;
MessageBox.Show(value.ToString());
}
}
}
class Person
{
public string Name1 { get; set; }
public string Name2 { get; set; }
public override string ToString()
{
return Name1 + "-" + Name2;
}
}
}
uj5u.com熱心網友回復:
集合系結之后,會傳遞到每個Item的DataContext上,因此肯定可以拿到~~~
模版雖然方便,但介于對于新手對面向物件的理解、重用等等就不是太友好,以及你的class屬性改變的時候,無法智能化跟隨自動變化。很多時候不推薦完全去用xmal方式。
我這里有個不常用的方式(如果你有所了解視圖實作模式那么就是很簡單)
public partial class ListBoxEx
{
public Func<ListBoxEx, UIElement> OnGenerateItem;
public int GenerateCounterOnStart { get; private set; }
public Action<ListBoxEx,DependencyPropertyChangedEventArgs> PropertyChanged { get; set; }
public ListBoxEx()
{
InitializeComponent();
}
protected override DependencyObject GetContainerForItemOverride()
{
GenerateCounterOnStart++;
var itemContent = OnGenerateItem?.Invoke(this);
return itemContent ?? new ListBoxItem(); ;
}
public ScrollViewerEx ScrollViewer { get; private set; }
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
ScrollViewer = this.GetTemplateChild("_cScrollViewer") as ScrollViewerEx;
}
public new IEnumerable ItemsSource
{
get => base.ItemsSource;
set
{
base.ItemsSource = value;
GenerateCounterOnStart = 0;
}
}
public int MaxItemsCount { get; set; } = 1000;
public bool IsReduceHead { get; set; }
protected override void OnItemsChanged(NotifyCollectionChangedEventArgs e)
{
base.OnItemsChanged(e);
while (MaxItemsCount > 0 && Items.Count > MaxItemsCount)
{
if (!IsReduceHead)
{
var item = Items[Items.Count - 1];
if(item is DependencyObject) item.ToType<DependencyObject>().GetAllChildren().OfType<Image>().ToList().ForEach(t=>t.Source = null);
Items.RemoveAt(Items.Count - 1);
}
else
{
var item = Items[0];
if (item is DependencyObject) item.ToType<DependencyObject>().GetAllChildren().OfType<Image>().ToList().ForEach(t => t.Source = null);
Items.RemoveAt(0);
}
}
}
protected override void OnPropertyChanged(DependencyPropertyChangedEventArgs e)
{
base.OnPropertyChanged(e);
PropertyChanged?.Invoke(this,e);
}
protected override AutomationPeer OnCreateAutomationPeer()
{
return new ControlSpecificCustomAutomationPeer(this);
}
}
對于ListBox.Items你是可以隨便插入顯示(包括string)物件實體的,要做到這個你還可以不用模版方法(如果Item控制元件顯示你可以系結也可以不系結,系結的話就要在ListBoxEx中傳遞自行寫)。
如:(這是ListBoxEx實體)_cListBox進入.OnGenerateItem = OnGenerateItem;
(WPF中ListBox中Items的控制元件很有意思,你可以自己Items.Add試試,然后看看原始碼,用原始碼中的控制元件你再試試,寫得挺復雜的)
如果系結不用模版怎么寫,當然是代碼了:
_c次數.SetBinding(IntegerUpDown.ValueProperty, ClassEx.GetPropertyName<霸屏踢人>(t => t.每分鐘極限次數));
這樣你在改變"每分鐘極限次數"這個屬性的時候,Vs就可以自動變名稱了,而且錯誤可以在編輯時就拋出來,而不是運行時你到處去找問題。
以上供爾參考學習~
uj5u.com熱心網友回復:
請問 怎么獲取item的index呢
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/68374.html
標籤:其他技術討論專區
