簡體   English   中英

如何獲取我創建的 WPF 元素的屬性

[英]How can I get properties of a WPF element I have created

我目前正在制作一個工具,允許用戶通過檢查他們想要在列表框中搜索哪些網站來快速進行多種類型的搜索。

我遇到了一個與向網站添加自定義搜索相關的問題——我想要一個按鈕,允許用戶將另一個復選框網站添加到包含它們的列表框。 這部分並不難,但是一旦我創建了復選框,我就需要獲取用戶輸入的站點的 URL,但我無法弄清楚如何引用新創建的復選框的任何方面。

目前,每當用戶單擊搜索按鈕時,程序都會檢查啟用了哪些站點,然后運行與所選搜索相關的任何代碼。 對於默認的自定義搜索,它獲取文本框的內容並使用 site:textbox.text(在 google 中)從該網站查找結果

我需要能夠引用新的自定義站點復選框的文本,然后從中創建一個 URL。 您可以在此處看到我創建了一個字符串數組,其中包含其他網站選擇的鏈接。 我試圖通過在創建新按鈕 (button_click_1) 的函數中添加 sites.Append(newcustomsitetext.text) 來解決這個問題,但此時用戶實際上還沒有進入自定義網站,並且該字符串在數組,因為我已經在數組 (google.com/searchq=etc) 中擁有了我需要的鏈接部分。

我還嘗試創建一個方法來附加選擇(updateSitesArray),但我再次無法在創建它的函數之外引用文本框的屬性。

我對 C# 和 WPF 非常缺乏經驗,所以我確定我在這里做錯了一些事情(可能很多事情),所以如果這是一個愚蠢的問題,我深表歉意。

這是應用程序C# 腳本的屏幕截圖(browserDiplay.DetermineURL 將字符串轉換為 webbrowser 的 URI)

namespace HomeWork_Helper
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    /// 
    
    public partial class MainWindow : Window
    {
        public int selection = 0;
        public ListBox list;
        string[] sites = { "https://www.google.com/search?q=","https://api.wolframalpha.com/v1/simple?i=","http://www.google.com/search?q=site:wikipedia.org","http://www.google.com/search?q=site:"};
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e) //main search button click
        {
            for (int i = 0; i < SitesList.Items.Count; i++)
            {
                string text = SitesList.Items[i].ToString();
                if (text.Contains("IsChecked:True"))
                {
                    if (SitesList.Items[i].Equals(WolframCheck))
                    {
                        BrowserDisplay browserDisplay = new BrowserDisplay(); //create a new browser
                        browserDisplay.Show(); //Show it
                        browserDisplay.determineURL(sites[i] + searchBox.Text + "&appid=2GA4A5-YL7HY9KR42");
                        Console.WriteLine(text);
                        Console.WriteLine((sites[i] + searchBox.Text + "&appid=2GA4A5-YL7HY9KR42"));
                    }
                    else if (SitesList.Items[i].Equals(CustomSiteCheck))
                    {
                        BrowserDisplay browserDisplay = new BrowserDisplay(); //create a new browser
                        browserDisplay.Show(); //Show it
                        browserDisplay.determineURL(sites[i] + CustomSiteEntry.Text + " " + searchBox.Text);
                        Console.WriteLine(text);
                    }
                    else if (i > 4)    //if i > 4 listbox item is new 
                    {
                        BrowserDisplay browserDisplay = new BrowserDisplay(); //create a new browser
                        browserDisplay.Show(); //Show it
                        browserDisplay.determineURL(sites[i] + ***How can I reference a new element here?*** + " " + searchBox.Text);
                        Console.WriteLine(text);
                    }
                    else
                    {
                        BrowserDisplay browserDisplay = new BrowserDisplay(); //create a new browser
                        browserDisplay.Show(); //Show it
                        browserDisplay.determineURL(sites[i] + " " + searchBox.Text);
                        Console.WriteLine(text);
                    }
                }
            }
            
        }
        
        public void Button_Click_1(object sender, RoutedEventArgs e) //creates a new buton
        {
            
            CheckBox newcustomcheck = new CheckBox();
            TextBox customsitetext = new TextBox();
            customsitetext.Width = 215;
            customsitetext.Text = "Enter a custom site here";
            newcustomcheck.Content = customsitetext;
            //newcustomcheck.Checked += updateSitesArray;
            SitesList.Items.Add(newcustomcheck);
            sites.Append(customsitetext.Text);
                
        }
        private void updateSitesArray(TextBox sender, EventArgs e)
        {
            
        }
    }
}

XAML 代碼:

<Window x:Class="HomeWork_Helper.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:HomeWork_Helper"
        mc:Ignorable="d"
        Title="MainWindow" Background="DarkGray" Height="450" Width="800">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="20"/>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="20"/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="20" />
            <RowDefinition Height="Auto" MinHeight="60"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="20" />
        </Grid.RowDefinitions>

        <TextBox Name="searchBox" HorizontalAlignment="Left" Height="60" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="501" Grid.Column="2" Grid.ColumnSpan="2" Grid.Row="1"/>
        <Button Name ="searchButton" Content="Search" HorizontalAlignment="Left" Height="60" VerticalAlignment="Top" Width="251" Grid.Column="1" Grid.Row="1" Click="Button_Click"/>

        <ListBox Name="SitesList" Grid.Column="1" HorizontalAlignment="Left" Height="160" Grid.Row="2" VerticalAlignment="Top" Width="251">
            <CheckBox Name="GoogleCheck" Content="Google Search" IsChecked="false"/>
            <CheckBox Name="WolframCheck" Content="Wolfram Search (slow)" IsChecked="false"/>
            <CheckBox Name="WikipediaCheck" Content="Wikipedia Search" IsChecked="false"/>
            <CheckBox Name="CustomSiteCheck" >
                <TextBox Name="CustomSiteEntry" Text="Enter a cutstom site here." Width="215"/>
            </CheckBox>
        </ListBox>
        <Button Content="Add another custom site" Grid.Column="1" HorizontalAlignment="Left" Height="40" Grid.Row="3" VerticalAlignment="Top" Width="250" Click="Button_Click_1"/>





    </Grid>
</Window>

切勿直接創建項目。 始終使用數據模型,並讓框架通過定義DataTemplate自動/動態地創建項目。 這將使數據處理更加容易,減少相關代碼並顯着提高可讀性。
始終對數據模型進行操作。 不要對視圖進行操作。

另一個重要的設計原則是避免檢查數據類型以執行相關操作的類型切換或鏈接的 if 語句。 此類代碼無法擴展:添加新數據類型會破壞您的代碼。 您還需要添加新的 if 語句。 代碼將變得丑陋且難以維護。

在您的情況下,只需封裝單個信息(或邏輯)並將其移動到專用類中,例如Site 每個Site知道正確的 URL 或如何組合它。 調用者不必關心這些細節。 只需獲取組合的 URL 並啟動瀏覽器即可執行搜索。 這也實現了干凈的多態性並刪除了重復的代碼:簡單地迭代Site項目並使用單個通用搜索實現基於每個Site數據開始搜索。

以下示例是如何開始使用原始模板。 既然你是用WPF開發,我建議你盡快學習和使用MVVM。 這將使開發變得更加容易。

請閱讀:
INotifyPropertyChanged (允許數據綁定)、 數據綁定概覽數據模板概覽(和依賴屬性概覽)以充分理解示例。

網站.cs

public class Site : INotifyPropertyChanged
{
  private string title;
  public string Title
  {
    get => this.title;
    set
    {
      this.title = value;
      OnPropertyChanged();
    }
  }

  private string url;
  public string Url
  {
    get => this.url;
    set
    {
      this.url = value;
      OnPropertyChanged();
    }
  }

  private bool isEnabled;
  public bool IsEnabled
  {
    get => this.isEnabled;
    set
    {
      this.isEnabled = value;
      OnPropertyChanged();
    }
  }

  public event PropertyChangedEventHandler PropertyChanged;
  protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    => this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}

主窗口.xaml.cs

public partial class MainWindow : Window
{
  // This property should really be a DependencyProperty
  public ObservableCollection<Site> SearchSites { get; }

  public MainWindow()
  {
    InitializeComponent();
    this.DataContext = this;

    this.SearchSites = new ObservableCollection<Site>
    {
      new Site() { Title = "DuckDuckGo", Url = "https://duckduckgo.com/?q=" }
    };
  }

  private void StartSearch_OnClick(object sender, RoutedEventArgs e)
  {
    IEnumerable<Site> enabledSearchSites = this.SearchSites.Where(site => site.IsEnabled);
    foreach (Site searchSite in enabledSearchSites)
    {
      var browserDisplay = new BrowserDisplay(); //create a new browser
      browserDisplay.Show(); //Show it
      browserDisplay.determineURL(searchSite.Url + this.SearchBox.Text);
    }
  }

  public void AddNewSearchSite_OnClick(object sender, RoutedEventArgs e)
  {
    var newSearchSite = new Site 
    { 
      Title = "Enter a custom site name here", 
      Url = "Enter a custom site URL here" 
    };

    // Since SearchSites is an ObservableCollection, adding/removing an item 
    // will instantly update the view (binding target)
    this.SearchSites.Add(newSearchSite);
  }
}

主窗口.xaml

<ListBox ItemsSource="{Binding SearchSites}">
  <ListBox.ItemTemplate>

    <!-- Describe how each item should look like 
         and wire it to the data model using data binding -->
    <DataTemplate DataType="{x:Type local:Site}">
      <CheckBox IsChecked="{Binding IsEnabled}">
        <CheckBox.Content>
          <StackPanel Orientation="Horizontal">
            <TextBox Text="{Binding Title}" />
            <TextBox Text="{Binding Url}" />
          </StackPanel>
        </CheckBox.Content>
      </CheckBox>
    </DataTemplate>
  </ListBox.ItemTemplate>
</ListBox>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM