私はC#でプロパティ(getとset)を学ぼうとしています。私はまだこの言語には触れていませんし、現在、写真でわかるように、いくつかのテキストボックスでWPFを使った簡単なプログラムを作っています。プロパティ(get; set;)をWPF User Controlに割り当てる方法は?
- 入力:
だから、ここに記述されているユーザは、メインウィンドウ
- 出力1に位置入力を、入力することができる場所、それがあります:それはメインウィンドウ に位置入力で入力した文字列を、参照する場所です
- 出力2:MainWindowのタブ1内にある出力1と同じです。
- 出力3:MainWindowのタブ2内にある出力1と同じです。
- 出力4:ローカルUserControl Page1を参照して、タブ2内にある出力1と同じです。
- ボタン:ボタンは、コードがある入力ここ
"保存" します メインウィンドウ:ソリューション - >私は
<!-- MainWindow.xaml -->
<Window x:Class="TestGetSet.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:TestGetSet"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<TabControl x:Name="tabControl" HorizontalAlignment="Left" Height="212" Margin="37,20,0,0" VerticalAlignment="Top" Width="447">
<TabItem Header="TabItem">
<Grid Background="#FFE5E5E5">
<TextBox x:Name="output2" HorizontalAlignment="Left" Height="23" Margin="156,76,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="120"/>
<Label x:Name="label2" Content="Output 2" HorizontalAlignment="Left" Margin="156,46,0,0" VerticalAlignment="Top"/>
</Grid>
</TabItem>
<TabItem Header="TabItem">
<Grid Background="#FFE5E5E5">
<TextBox x:Name="output3" HorizontalAlignment="Left" Height="23" Margin="321,26,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120"/>
<local:Page1 x:Name="Page1"/>
<Label x:Name="label3" Content="Output 3" HorizontalAlignment="Left" Margin="321,0,0,0" VerticalAlignment="Top"/>
</Grid>
</TabItem>
</TabControl>
<TextBox x:Name="input" Text=""/>
<TextBox x:Name="output1" Text=""/>
<Button x:Name="button" Content="Button" Click="button_Click"/>
<Label x:Name="label" Content="Input" HorizontalAlignment="Left" Margin="37,239,0,0" VerticalAlignment="Top"/>
<Label x:Name="label1" Content="Output 1" HorizontalAlignment="Left" Margin="364,239,0,0" VerticalAlignment="Top"/>
</Grid>
local:page1
x:Name="Page1"
を追加したメインウィンドウのコードの後ろに:ソリューション - - >次にPage1.xaml(変更なしM 1つのライン
Page1.passingvalue(..)
// MainWindow.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
public partial class MainWindow : Window
{
public myProperty myProp = new myProperty();
public MainWindow()
{
InitializeComponent();
}
private void button_Click(object sender, RoutedEventArgs e)
{
myProp.myData = input.Text;
output1.Text = myProp.myData;
output2.Text = myProp.myData;
output3.Text = myProp.myData;
Page1.passingvalue(myProp.myData);
}
}
を添加解決のためにここにADE)の背後にある
<!-- Page1.xaml-->
<UserControl x:Class="TestGetSet.Page1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:TestGetSet"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
<TextBox x:Name="output4" Text=""/>
<Label x:Name="label" Content="Output 4" HorizontalAlignment="Left" Margin="92,110,0,0" VerticalAlignment="Top"/>
</Grid>
</UserControl>
のPage1コード:ソリューション - >タイマーを削除し、passingvalue
// Page1.xaml.cs
using System;
using System.Windows.Controls;
using System.Windows.Threading;
using System.Threading;
using System.ComponentModel;
namespace TestGetSet
{
public partial class Page1 : UserControl
{
private Thread _receiveThread;
myProperty myProp = new myProperty();
public Page1()
{
InitializeComponent();
/*DispatcherTimer MyTimer = new DispatcherTimer();
MyTimer.Interval = new TimeSpan(0, 0, 0, 0, 100);
MyTimer.Tick += MyTimer_Tick;
MyTimer.Start();*/
}
public void passingvalue(string m)
{
output4.Text = m;
}
/*private void MyTimer_Tick(object sender, EventArgs e)
{
output4.Text = myProp.myData;
}*/
}
}
を追加した最後のものを、財産、簡易版:
// myProperty.cs
namespace TestGetSet
{
public class myProperty
{
public string myData { get; set }
}
}
プロパティINotifyPropertyChanged:
// myProperty.cs
using System.ComponentModel;
namespace TestGetSet
{
public class myProperty : INotifyPropertyChanged
{
private string _textdata;
public string myData {
get
{
return _textdata;
}
set
{
_textdata = value;
NotifyPropertyChanged("myData");
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
}
ここ
は、プログラムウィンドウのスクリーンショットです:あなたが見ることができるように one と two
、唯一OUTPUT4は空で、他の出力はまだ私はINotifyPropertyChangedのを使用していないにもかかわらず、結果私を与えます。私の質問はなぜ、そして私はこれをどのように修正できますか? Output4が入っているPage1のUserControlを使用しているからだと思います。私は答えを探していましたが、何も出てこなかったのです。どんな助けも大歓迎です。ありがとうございました。
コードを更新しました。それは今働いている。 passingvalue
の参照先はHow to Pass a Value From a Window to a UserControl in WPF
ありがとうございます。
は全体のXAMLを提供します。誰もあなたのコードに不足している部分を追加することはありません。 –
page1のコードビハインド内で定義されたプロパティ 'myprop'は、 'myProperty'クラスの新しいインスタンスであるため、空です。 MainWindowとPage1で定義されたmyProperty型のプロパティは、同じ名前の2つのプロパティですが、同じクラスの異なるインスタンスです。 mainwindowとpage1の間でプロパティを "共有"する場合は、データバインディングhttps://msdn.microsoft.com/en-us/library/ms752347(v=vs.110).aspxの使用を検討する必要があります。 – CiccioRocca
@NareshRavlaniの全コードが追加されました。ごめんなさい。 –