私はWPFとMVVMを学習しており、学習実習プログラムとしてWPFに単純なタイプ選択リストボックスを書いています。 私は以下の3つの質問があります。 -WPFタイプリストボックスを選択クエリ
1)DataBinding経由でTxtMailのTextプロパティを設定するには?DataBinding.Iを使用してXAMLのTextプロパティを設定すると、現在他のロジックが壊れています。コードビハインドから直接Txtmailのテキストプロパティを設定するのではなく、XAMLのDataBindingを介して同じものを設定することで、対象の制限された理解のために物事が乱れてしまいます。
2)AllMatchingというListBoxのItemSourceは、テキスト検索パターンでプログラム的に変更されているため、Code-Behindから設定されています.XAMLからどのように設定できますか?
3)GUIコントロールイベントのロジックを削除してXAMLに同じ方法を含める方法はありますか? コード全体は下の通りである: -
ビューモデル:
public class VM_Data : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public int p_ID;
public double p_SP, p_CP;
public string p_Name;
public List<DM_Data> AllData;
public List<DM_Data> DynamicData;
public Visibility p_ListVisibility = Visibility.Collapsed;
private DM_Data _currentRec;
public DM_Data CurrentRec
{
get { return _currentRec; }
set { _currentRec = value; RaisePropertyChangedEvent("CurrentRec"); }
}
public VM_Data()
{
LoadData();
}
public int ID
{
get { return p_ID; }
set
{
if (p_ID != value)
{
RaisePropertyChangedEvent("ID");
p_ID = value;
}
}
}
public double SP
{
get { return p_SP; }
set
{
if (p_SP != value)
{
RaisePropertyChangedEvent("SP");
p_SP = value;
}
}
}
public double CP
{
get { return p_CP; }
set
{
if (p_CP != value)
{
RaisePropertyChangedEvent("CP");
p_CP = value;
}
}
}
public Visibility ListVisibility
{
get { return p_ListVisibility; }
set
{
p_ListVisibility = p_ListVisibility != value ? value : p_ListVisibility;
RaisePropertyChangedEvent("ListVisibility");
}
}
public string Name
{
get { return p_Name; }
set
{
if (p_Name != value)
{
RaisePropertyChangedEvent("Name");
p_Name = value;
}
}
}
private void LoadData()
{
AllData = new List<DM_Data>();
DynamicData = new List<DM_Data>();
string[] strNames = "Jatinder;Shashvat;shashikala;shamsher;shahid;justin;jatin;jolly;ajay;ahan;vijay;suresh;namita;nisha;negar;zenith;zan;zen;zutshi;harish;hercules;harman;ramesh;shashank;mandeep;aman;amandeep;amarjit;asim;akshay;amol;ritesh;ritivik;riz;samana;samaira;bhagwandass;bhagwan;bhawna;bhavna".Split(';');
for(int i=0;i<=strNames.GetUpperBound(0);i++)
{
DM_Data NewRec = new DM_Data();
NewRec.CP = new Random().Next(200, 400);
NewRec.SP = new Random().Next(1, 10);
NewRec.ID = i + 1;
NewRec.Name = strNames[i];
AllData.Add(NewRec);
}
AllData = DynamicData = AllData.OrderBy(item => item.Name).ToList();
}
private void RaisePropertyChangedEvent(string Property)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(Property));
}
}
}
のDataModel:
public class DM_Data
{
public int p_ID;
public double p_SP, p_CP;
public string p_Name;
public Visibility p_ListVisibility = Visibility.Visible;
public int ID
{
get { return p_ID; }
set { p_ID = value; }
}
public Visibility ListVisibility
{
get { return p_ListVisibility; }
set { p_ListVisibility = value; }
}
public double SP
{
get { return p_SP; }
set { p_SP = value; }
}
public double CP
{
get { return p_CP; }
set { p_CP = value; }
}
public string Name
{
get { return p_Name; }
set { p_Name = value; }
}
}
MainWindow.Xaml
<Window x:Class="TypeSelect.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:TypeSelect"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<StackPanel>
<Canvas>
<TextBox x:Name="TxtMail" Width="244" FontSize="14" Canvas.Left="36" Canvas.Top="34" Height="25" GotFocus="TxtMail_GotFocus" LostFocus="TxtMail_LostFocus" KeyUp="TxtMail_KeyUp" MouseUp="TxtMail_MouseUp" />
<ListBox x:Name="AllMatching" Width="{Binding ElementName=TxtMail,Path=Width}" MinHeight="10" MaxHeight="100" Canvas.Top="54" Canvas.Left="36" DisplayMemberPath="Name" SelectedItem="{Binding CurrentRec,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" SelectedValue="Name" SelectedValuePath="Name" Visibility="{Binding ListVisibility,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" KeyUp="AllMatching_KeyUp" ScrollViewer.VerticalScrollBarVisibility="Auto" ScrollViewer.HorizontalScrollBarVisibility="Auto" MouseUp="AllMatching_MouseUp" GotFocus="AllMatching_GotFocus" MouseDoubleClick="AllMatching_MouseDoubleClick" ForceCursor="True">
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Style.Triggers>
<Trigger Property="IsSelected" Value="True" >
<Setter Property="FontWeight" Value="Bold" />
<Setter Property="FontFamily" Value="Arial Bold" />
<Setter Property="Background" Value="LightGray" />
<Setter Property="Foreground" Value="Black" />
<Setter Property="FontSize" Value="18" />
</Trigger>
</Style.Triggers>
<Style.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="LightGray"/>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="LightGray"/>
<SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="White" />
</Style.Resources>
</Style>
</ListBox.ItemContainerStyle>
</ListBox>
<Button Content="Test" x:Name="cmdtest" Click="cmdtest_Click"/>
</Canvas>
</StackPanel>
のMainWin dow.Xaml.cs
namespace TypeSelect
{
public partial class MainWindow : Window
{
VM_Data ViewModel;
DM_Data Existing;
public MainWindow()
{
InitializeComponent();
ViewModel = new VM_Data();
this.DataContext = ViewModel;
AllMatching.ItemsSource = ViewModel.DynamicData;
ViewModel.CurrentRec = Existing= ViewModel.AllData[new Random().Next(0, ViewModel.AllData.Count - 1)];
TxtMail.Text = (ViewModel.CurrentRec != null) ? ViewModel.CurrentRec.Name : "";
}
private void cmdtest_Click(object sender, RoutedEventArgs e)
{
}
private void TxtMail_KeyUp(object sender, KeyEventArgs e)
{
if (e.Key.Equals(Key.Enter))
{
ViewModel.CurrentRec = (DM_Data)AllMatching.SelectedItem;
((TextBox)sender).Text = (ViewModel.CurrentRec != null) ? ViewModel.CurrentRec.Name : "";
//ViewModel.CurrentRec.SearchText = ViewModel.CurrentRec.Name;
ViewModel.ListVisibility = ViewModel.ListVisibility.Equals(Visibility.Collapsed) ? Visibility.Visible : Visibility.Collapsed;
if (ViewModel.ListVisibility.Equals(Visibility.Visible))
{
AllMatching.ScrollIntoView(ViewModel.CurrentRec);
}
else
{
Existing = ViewModel.CurrentRec;
}
}
else if (e.Key.Equals(Key.Escape))
{
ViewModel.CurrentRec = Existing;
ViewModel.ListVisibility = Visibility.Collapsed;
((TextBox)sender).Text = (ViewModel.CurrentRec != null) ? ViewModel.CurrentRec.Name : "";
}
else if (e.Key.Equals(Key.Up))
{
AllMatching.SelectedIndex = AllMatching.SelectedIndex > 0 ? AllMatching.SelectedIndex - 1 : AllMatching.SelectedIndex;
ViewModel.CurrentRec = (DM_Data)AllMatching.SelectedItem;
AllMatching.ScrollIntoView(ViewModel.CurrentRec);
}
else if (e.Key.Equals(Key.Down))
{
AllMatching.SelectedIndex = AllMatching.SelectedIndex < AllMatching.Items.Count ? AllMatching.SelectedIndex + 1 : AllMatching.SelectedIndex;
ViewModel.CurrentRec = (DM_Data)AllMatching.SelectedItem;
AllMatching.ScrollIntoView(ViewModel.CurrentRec);
}
else
{
ViewModel.DynamicData = ViewModel.AllData.Where(item => item.Name.StartsWith(TxtMail.Text, StringComparison.OrdinalIgnoreCase)).ToList<DM_Data>();
AllMatching.ItemsSource = ViewModel.DynamicData;
ViewModel.CurrentRec = (AllMatching.Items.Count > 0) ? ViewModel.DynamicData.Where(item => item.Name.StartsWith(TxtMail.Text, StringComparison.OrdinalIgnoreCase)).FirstOrDefault() : null;
if (AllMatching.Visibility.Equals(Visibility.Collapsed))
{
AllMatching.Visibility = Visibility.Visible;
AllMatching.ScrollIntoView(ViewModel.CurrentRec);
}
}
}
private void TxtMail_GotFocus(object sender, RoutedEventArgs e)
{
DM_Data Existing = ViewModel.CurrentRec;
ViewModel.ListVisibility = Visibility.Visible;
}
private void TxtMail_LostFocus(object sender, RoutedEventArgs e)
{
ViewModel.ListVisibility = Visibility.Collapsed;
}
private void TxtMail_MouseUp(object sender, MouseButtonEventArgs e)
{
ViewModel.ListVisibility = Visibility.Visible;
AllMatching.ScrollIntoView(ViewModel.CurrentRec);
}
private void AllMatching_MouseUp(object sender, MouseButtonEventArgs e)
{
ViewModel.CurrentRec = (DM_Data)AllMatching.SelectedItem;
ViewModel.ListVisibility = Visibility.Visible;
AllMatching.ScrollIntoView(ViewModel.CurrentRec);
}
private void AllMatching_GotFocus(object sender, RoutedEventArgs e)
{
ViewModel.ListVisibility = Visibility.Visible;
}
private void AllMatching_KeyUp(object sender, KeyEventArgs e)
{
if (e.Key.Equals(Key.Enter))
{
TxtMail.Text = ViewModel.CurrentRec.Name;
ViewModel.ListVisibility = Visibility.Collapsed;
}
}
private void AllMatching_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
ViewModel.CurrentRec = (DM_Data)AllMatching.SelectedItem;
TxtMail.Text = (ViewModel.CurrentRec != null) ? ViewModel.CurrentRec.Name : "";
//ViewModel.CurrentRec.SearchText = ViewModel.CurrentRec.Name;
ViewModel.ListVisibility = ViewModel.ListVisibility.Equals(Visibility.Collapsed) ? Visibility.Visible : Visibility.Collapsed;
if (ViewModel.ListVisibility.Equals(Visibility.Visible))
{
AllMatching.ScrollIntoView(ViewModel.CurrentRec);
}
else
{
Existing = ViewModel.CurrentRec;
}
}
}
}
:tx.Iは最初の2つのコマンドを実装しています。私はすぐに読んで、questions.plzを投稿します。 –