내 프로그램에서 두 개의 창이 동시에 열리 며, 하나는 WPF 양식이고 다른 하나는 WinForms 양식입니다.WinForms 폼의 단추를 클릭하여 WPF 폼의 레이블 배경색을 변경하려면 어떻게해야합니까?
WinForms 폼에있는 단추를 클릭하여 WPF 폼의 레이블 배경색을 변경해야합니다.
어떻게하면됩니까?
내 프로그램에서 두 개의 창이 동시에 열리 며, 하나는 WPF 양식이고 다른 하나는 WinForms 양식입니다.WinForms 폼의 단추를 클릭하여 WPF 폼의 레이블 배경색을 변경하려면 어떻게해야합니까?
WinForms 폼에있는 단추를 클릭하여 WPF 폼의 레이블 배경색을 변경해야합니다.
어떻게하면됩니까?
당신은 당신의 WinForms
형태로 Button
컨트롤을 눌렀을 때 이벤트 통지를 게시하는 EventHandler
을 설정할 수 있습니다. 이 event
은 WPF
Window/UserControl
에 의해 모니터링되어 event
이 트리거 된 시점을 감지 한 다음 원하는 color
에 WPF
Label
을 설정하도록 요청합니다.
MainWindow.xaml
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
DataContext="{Binding RelativeSource={RelativeSource Self}}"
Title="MainWindow" Height="150" Width="225">
<Grid>
<Label Content="Label" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Height="100" Width="197" Background="{Binding LabelBgColour}"/>
</Grid>
</Window>
MainWindow.xaml.cs를
namespace WpfApplication1
{
using System.Windows;
using System.ComponentModel;
using System.Windows.Media;
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window, INotifyPropertyChanged
{
public MainWindow()
{
InitializeComponent();
var f = new Form1();
f.ChangedColourEventHandler += TriggeredChangedColourEventHandler;
f.Show();
}
private Brush labelBgColour;
public Brush LabelBgColour
{
get
{
return this.labelBgColour;
}
set
{
this.labelBgColour = value;
OnPropertyChanged();
}
}
private void TriggeredChangedColourEventHandler(object sender, Brush color)
{
this.LabelBgColour = color;
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName = null)
{
var handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
}
Form1.cs를
namespace WpfApplication1
{
using System;
using System.Windows.Forms;
using System.Windows.Media;
public partial class Form1 : Form
{
public EventHandler<Brush> ChangedColourEventHandler;
public Form1()
{
InitializeComponent();
}
private Brush bgColour;
private Brush BgColour
{
get
{
return this.bgColour;
}
set
{
this.bgColour = value;
TriggerChangedColourEventHandler(value);
}
}
private void button1_Click(object sender, EventArgs e)
{
this.BgColour = (Brush)new BrushConverter().ConvertFromString("#FF00B25A");
}
private void TriggerChangedColourEventHandler(Brush color)
{
var handler = ChangedColourEventHandler;
if (handler != null)
{
handler(this, color);
}
}
}
}
0,123,751,546,468,에는 button1
이라는 이름의 단일 Button
컨트롤이 있습니다 (예제 cs에서 확인할 수 있음). 간결함을 위해 Form1.Design.cs
을 생략했습니다.
이 예제에서는 으로 이동하여 원하는 Color
값을 얻습니다. Color
값이 EventHandler
이벤트 (EventArgs
)의 일부로 전송되도록 쉽게 적용 할 수 있습니다.
이벤트가 발생시키는 메서드의 인수로 이벤트를 전달하도록하여 코드를 단순화 할 수 있으므로 처리기는 색상을 가져 오기 위해 양식으로 다시 이동할 필요가 없습니다. 가장 큰 장점은 WPF 창에서 폼을 인스턴스 필드로 유지하는 대신 폼을 잊을 수 있다는 것입니다. 그러나 일반적인 접근 방식은 정확합니다. – Servy
이 답변에서 발견 한 유일한 것은'BrushConverter'입니다. OP는 16 진수 색상 문자열을'Color'로 변환하는 방법을 알고 싶어하므로 코드가 너무 중복되어 OP를 혼동시킬 수 있습니다. –
@Servy 그게 내가 보통 취하는 방법이고, 원하는 정보를 이벤트의 일부로 전달합니다. 웬일인지 나는이 예에서 그렇게 보이지 않기로 결정했다. 귀하의 의견에 비추어 나는 수정안을 만들었습니다. –
나는 이것을 달성하는 많은 방법이있을 것이라고 확신한다. 더 작고 더 어둡거나 좀 더 길어 지지만 더 분명합니다.
Win32 API를 통해 뭔가 할 수있는 것처럼, 메모리 매핑 파일을 통해 통신하거나 NamedPipes 또는 MSMQ와 같은 통신 전송을 사용하십시오.
행운을 빕니다 :
당신은 아이디어 이러한 리소스 중 일부를 볼 수 있습니다! :)
WPF 폼에서 Winform 개체를 사용할 수 있고 Button1이 공개되어 있다고 가정하면 WPF 양식에서 아래와 비슷한 코드를 추가해야합니다.
WinForm.Button1.Click += new System.EventHandler(Button1_Click);
private void Button1_Click(object sender, EventArgs e)
{
// Change label background
}
실제로 내가 찾고 있던 것은 당신이 말하지 않은 것입니다 (// 레이블 배경 변경). 방금 코드가 wpfform.label1.Background라는 것을 발견했습니다. System.Windows.Media.Brushes.DarkRed; 하지만이 같은 특정 색상을 사용해야합니다 # FF00B25A – user2558874
당신은 정말로 당신의 문제를 해결하는 데 더 많은 시간을 소비하는 법을 배워야합니다. 당신이 가지고 있지 않은 문제를 풀기 위해 여러 사람들이 시간 낭비를 일으켰습니다. – Mithon
@ user2558874 해결책에 대한 내 대답을 참조하십시오. –
같은 프로그램입니까? 그렇다면, 다른 폼에 하나의 폼에 대한 객체 참조가 필요하다. – gunr2171
예, 동일한 프로그램입니다. 둘 다 winforms 양식이라면 form2.label1을 할 수 있습니다.backcolor,하지만 WPF 배경색을 변경하는 방법을 모르겠습니다. – user2558874
이런 종류의 응용 프로그램은 재미 있습니다. 나는 당신이'Command Prompt '와 같은 것들을 더 섞어 놓아야한다고 생각하니? 그들과 함께 춤을합시다.) –