Почему не отображается новое значение у Label moneyBalance?
Проверяю полученное значение в MessageBox - оно там есть. Но когда добавляю в контент лейбла - ничего не выводится в нем (смотреть в обработчик Test_Click).
MainWindow:
<Window x:Class="MyWallet.View.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:MyWallet"
xmlns:vm="clr-namespace:MyWallet.ViewModel"
mc:Ignorable="d"
Title="MainWindow" Height="800" Width="1400" WindowStartupLocation="CenterScreen">
<Window.DataContext>
<vm:MainViewModel/>
</Window.DataContext>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="350"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Border Background="Transparent">
<StackPanel Orientation="Vertical" Background="Transparent">
<Button Content="Главная" Command="{Binding OpenMainPage}" Click="Test_Click"/>
<Button Content="Валюта" Command="{Binding OpenCurrencyConverterPage}"/>
</StackPanel>
</Border>
<Grid x:Name="InfoTable" Grid.Column="1">
<Frame Grid.RowSpan="2" NavigationUIVisibility="Hidden" Content="{Binding CurPage}"/>
</Grid>
</Grid>
.cs:
namespace MyWallet.View
{
public partial class MainWindow : Window
{
public static MainWindow MainWindowLink { get; private set; }
public MainWindow()
{
InitializeComponent();
MainWindowLink = this;
}
private void Test_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show(MainMenu.link.CurrencyConverterChangeCur);//просто пробовал устанавливать еще одним способом и проверял получает ли переменная значение
MessageBox.Show(MainMenu.link.moneyBalance.Content.ToString());//видно что месседж бокс вывел установленное значение и оно правильно, но на экране в лейбле все так же "123"
}
}
}
MainMenu:
<Page x:Class="MyWallet.View.MainMenu"
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:lvc="clr-namespace:LiveChartsCore.SkiaSharpView.WPF;assembly=LiveChartsCore.SkiaSharpView.WPF"
xmlns:local="clr-namespace:MyWallet.View"
mc:Ignorable="d"
d:DesignHeight="1000" d:DesignWidth="1200"
Title="MainMenu" Background="White">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="1*"/>
<RowDefinition Height="1.15*"/>
</Grid.RowDefinitions>
<TabControl>
<TabItem x:Name="tabItem1" Header="Расходы" Margin="0 0 0 -5">
<Grid>
<Border>
<!--<Button Content="123" Click="Test_Click"/>-->
<Label x:Name="moneyBalance" Content="123" Width="100" Height="100" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="16" Panel.ZIndex="1"/>
</Border>
</Grid>
</TabItem>
</TabControl>
</Grid>
.cs:
namespace MyWallet.View
{
public partial class MainMenu : Page
{
public static MainMenu link { get; private set; }
public MainMenu()
{
InitializeComponent();
link = this;
}
public string CurrencyConverterChangeCur
{
get { return CurrencyConverter.CurrencyConverterLink?.ChangeCur ?? ""; }
}
}
}
CurrencyWindow:
<Window x:Class="MyWallet.View.CurrencyWindow"
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:MyWallet.View"
mc:Ignorable="d"
Title="CurrencyWindow" Height="600" Width="500">
<Grid>
<StackPanel>
<ComboBox x:Name="CurrencyComboBox" Text="Выбор валюты" Width="250" Height="35" SelectedIndex="0" FontSize="20" Margin="0 10 0 0">
<ComboBoxItem Content="Выбор валюты"/>
<ComboBoxItem Name="BYN">
<StackPanel Orientation="Horizontal">
<TextBlock Text="Белорусский рубль"/>
</StackPanel>
</ComboBoxItem>
<ComboBoxItem Name="USD">
<StackPanel Orientation="Horizontal">
<TextBlock x:Name="dollarUSA" Text="Доллар США"/>
</StackPanel>
</ComboBoxItem>
<ComboBoxItem Name="EUR">
<StackPanel Orientation="Horizontal">
<TextBlock x:Name="euroEUR" Text="Евро"/>
</StackPanel>
</ComboBoxItem>
</ComboBox>
</StackPanel>
</Grid>
</Window>
.cs:
namespace MyWallet.View
{
public partial class CurrencyWindow : Window
{
public CurrencyWindow()
{
InitializeComponent();
}
public string GetCurrency()
{
ComboBoxItem selectedItem = (ComboBoxItem)CurrencyComboBox.SelectedItem;
string selectedName = selectedItem.Name;
switch (selectedName)
{
case "USD":
return "USD";
case "BYN":
return "BYN";
case "EUR":
return "EUR";
}
return "";
}
}
}
CurrencyConverter:
<Page x:Class="MyWallet.View.CurrencyConverter"
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:MyWallet.View"
mc:Ignorable="d"
d:DesignHeight="800" d:DesignWidth="1200"
Title="CurrencyConverter" Background="#EBFFFF">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="300"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Border Grid.Row="0" Background="White">
<Grid>
<Border>
<TextBlock Text="Валюта по умолчанию: " FontSize="25" VerticalAlignment="Center" Margin="50 0 0 0"/>
</Border>
<Border>
<Label x:Name="CurrText" Content="BYN" FontSize="25" VerticalAlignment="Center" Margin="330 0 0 0"/>
</Border>
<Border>
<Button Content="Изменить" Width="250" Height="80" VerticalAlignment="Center" HorizontalAlignment="Right" Margin="0 0 100 0" Click="ChangeCurrencyButton_Click"/>
</Border>
</Grid>
</Border>
</Grid>
</Page>
.cs:
namespace MyWallet.View
{
public partial class CurrencyConverter : Page
{
public static CurrencyConverter CurrencyConverterLink { get; private set; }
public CurrencyConverter()
{
InitializeComponent();
CurrencyConverterLink = this;
}
private string? changeCur;
public string? ChangeCur
{
get { return changeCur; }
}
private void ChangeCurrencyButton_Click(object sender, RoutedEventArgs e)
{
CurrencyWindow converterAddWindow = new CurrencyWindow();
bool? result = converterAddWindow.ShowDialog();
string oldCurr = CurrText.Content.ToString();
if (result == false)
{
changeCur = converterAddWindow.GetCurrency();
CurrText.Content = changeCur;
MainMenu.link.moneyBalance.Content = changeCur; // установил значение moneyBalance
}
}
}
}
MainViewModel:
namespace MyWallet.ViewModel
{
internal class MainViewModel : ViewModedBase
{
private Page MainMenuPage = new MainMenu();
private Page CurrencyConverterPage = new CurrencyConverter();
private Page _CurPage = new MainMenu();
public Page CurPage
{
get => _CurPage;
set => Set(ref _CurPage, value);
}
public ICommand OpenMainPage
{
get
{
return new RelayCommand(()=> CurPage = MainMenuPage);
}
}
public ICommand OpenCurrencyConverterPage
{
get
{
return new RelayCommand(() => CurPage = CurrencyConverterPage);
}
}
}
}
ViewModelBase:
namespace MyWallet.ViewModel
{
internal abstract class ViewModedBase : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void onPropertyChanged([CallerMemberName] string PropertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(PropertyName));
}
protected virtual bool Set<T>(ref T field, T value, [CallerMemberName] string PropertyName = null)
{
if (Equals(field, value)) return false;
field = value;
onPropertyChanged(PropertyName);
return true;
}
}
}