У меня есть окно, в нем есть combobox в котором я выбираю размер шрифта для текста в окне.
Я хочу привязать FontSize к значению из viewModel. TextBlock'и меняют размер шрифта, а элементы ListBox'a нет.
Класс modelView:
public class modelVeiw : INotifyPropertyChanged
{
const int F1big = 20;
const int F1mid = F1big - 4;
const int F1small = F1big - 8;
const int F2big = 18;
const int F2mid = F2big - 4;
const int F2small = F2big - 8;
const int F3big = 16;
const int F3mid = F3big - 4;
const int F3small = F3big - 8;
public ObservableCollection<string> comboItems { get; set; }
private string selectedbox;
public string SelectedBox
{
get { return selectedbox; }
set
{
selectedbox = value;
OnPropertyChanged("SelectedBox");
}
}
#region MyRegion
private int big;
public int Big
{
get { return big; }
set
{
big = value;
OnPropertyChanged("Big");
}
}
private int med;
public int Med
{
get { return med; }
set
{
med = value;
OnPropertyChanged("Med");
}
}
private int small;
public int Small
{
get { return small; }
set
{
small = value;
OnPropertyChanged("Small");
}
}
private List<Person> llist;
public List<Person> LList
{
get { return llist; }
set
{
llist = value;
OnPropertyChanged("LList");
}
}
#endregion
private RelayCommand selectedCommand;
public RelayCommand SelectedCommand
{
get
{
return selectedCommand ??
(selectedCommand = new RelayCommand(obj =>
{
string sel = obj as String;
if (sel != null)
{
if (sel == "Большой шрифт")
{
Big = F1big;
Med = F1mid;
Small = F1small;
}
else if (sel == "Средний шрифт")
{
Big = F2big;
Med = F2mid;
Small = F2small;
}
else if (sel == "Маленький шрифт")
{
Big = F3big;
Med = F3mid;
Small = F3small;
}
}
},
(obj) => true));
}
}
public modelVeiw()
{
comboItems = new ObservableCollection<string>();
comboItems.Add("Большой шрифт");
comboItems.Add("Средний шрифт");
comboItems.Add("Маленький шрифт");
SelectedBox = comboItems[0];
Big = F1big;
Med = F1mid;
Small = F1small;
LList = new List<Person>()
{
new Person {Name = "Иван", Lastname ="Иванов" , Age =10 },
new Person {Name = "Петр", Lastname ="Петров" , Age =15 },
new Person {Name = "Артем", Lastname ="Артемов" , Age =20 }
};
}
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged([CallerMemberName]string prop = "")
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(prop));
}
}
public class Person
{
public string Name { get; set; }
public string Lastname { get; set; }
public int Age { get; set; }
}
Xaml-разметка
<Window x:Class="Font_size.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:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
xmlns:local="clr-namespace:Font_size"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<StackPanel>
<ComboBox Name="combobox" ItemsSource="{Binding comboItems}" SelectedItem="{Binding SelectedBox}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectionChanged">
<i:InvokeCommandAction Command="{Binding SelectedCommand}" CommandParameter="{Binding SelectedBox}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</ComboBox>
<StackPanel>
<TextBlock Margin ="5" Text="Большой" FontSize="{Binding Big}"></TextBlock>
<TextBlock Margin ="5" Text="Средний" FontSize="{Binding Med}"></TextBlock>
<TextBlock Margin ="5" Text="Маленький" FontSize="{Binding Small}"></TextBlock>
<ListBox Name="list" ItemsSource="{Binding LList}" >
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Height="auto">
<TextBlock Text="{Binding Name}" FontSize="{Binding Big}"></TextBlock>
<TextBlock Text="{Binding Lastname}" FontSize="{Binding Med}"></TextBlock>
<TextBlock Text="{Binding Age} FontSize="{Binding Small}""></TextBlock>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
</StackPanel>