Clickイベントのコマンドバインディングinstedを使用してボタンを押して、MyView.xaml.csファイル内のコードを避けるために、キャンバスの色を変更しようとしました。コマンドが起動し、メッセージボックスに変更前と変更後の正しいカラーコード値が表示されるので、新しい色は設定されていますが、キャンバスの色は変更されません。 ClickイベントとMyView.xaml.csの背後にあるコードをすべてうまく動作させるよりもうまく動作しますが、MyView.xaml.csファイルの中にコマンドバインドとコードなしで動作させたいと思います。どうすればいいのですか?私は正しいことをしていません。他のクラスの色を変更できない
MainWindow.xaml
<Window x:Class="WpfApp1.MainWindow"
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:View="clr-namespace:WpfApp1.View"
xmlns:ViewModel="clr-namespace:WpfApp1.ViewModel"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525" Background="YellowGreen"
WindowStartupLocation="CenterScreen">
<Window.DataContext>
<ViewModel:MyClass/>
</Window.DataContext>
<View:MyView/>
</Window>
MyView.xamlファイル
<UserControl x:Class="WpfApp1.View.MyView"
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:ViewModel="clr-namespace:WpfApp1.ViewModel"
xmlns:View="clr-namespace:WpfApp1.View">
<Canvas Name="MainCanvas" Height="300" Width="502" Background="#FF148B63">
<Button Name="ChangeColorButton" Command="{Binding CommandChangeColor}" Content="new color" Height="25" Width="55" Margin="225,268,225,10"/>
</Canvas>
</UserControl>
MyClass.cs
public class MyClass : MyView
{
List<Color> _listOfColors = new List<Color>();
public MyClass(){ MyInitColor(); }
public ICommand CommandChangeColor
{
get { return new MyCommand(ChangeColor); }
}
public void ChangeColor()
{
MessageBox.Show("Color before: " + MainCanvas.Background.ToString());
Random rnd = new Random();
int i = rnd.Next(_listOfColors.Count - 1);
MainCanvas.Background = new SolidColorBrush(_listOfColors[i]);
MessageBox.Show("Color after: " + MainCanvas.Background.ToString());
}
}
タイ、私はあなたが提案し、プロパティを使用し、タトゥーに背景色をバインドし、完璧に働いたようでした:)私はここで新しいので、もう一つ質問があります。あなたの答えを正しいものとしてどのようにマークすることができますか? – Rookie
彼の答えの左側にあるティックサインをクリックするだけです。 – tabby
Nvm私はそれを行う方法を罰金:) – Rookie