I'm working on a WPF project using MVVM and I'm trying to implement a feature that changes the theme dynamically. The theming info is located in separate xaml files (ie Theme1.xaml, Theme2.xaml). I want to do the actual theme changing in the ViewModel class rather than in the code behind file of View.xaml for various reasons.
I've tried a couple ideas but can't get anything to work:
I tried binding the ResourceDictionary of View to a variable in ViewModel but am told that a binding cannot be set on the Source property of type ResourceDictionary
I don't have any sort of View object in my ViewModel class on which to call a "UpdateTheme" method
Any ideas on how I can change the MergedDictionary reference in my View class from the ViewModel class?
Thanks!
I have worked with the same time problem earlier here what i did in my case may be it can help you out.
Copy all you theme files (theme1.xaml, theme2.xaml...) into Themes folder at you exe path. and try with below sample code. using Bindings
C#:
private void ChangeTheme(FileInfo _SelectTheme)
{
App.Current.Resources.Clear();
App.Current.Resources.Source = new Uri(_SelectTheme.FullName, UriKind.Absolute);
}
private ObservableCollection<FileInfo> _files;
public ObservableCollection<FileInfo> Files
{
get { return _files; }
set { _files = value; OnChanged("Files"); }
}
public MainWindow()
{
this.InitializeComponent();
Dispatcher.BeginInvoke(DispatcherPriority.Background, new Action(() =>
{
var localthemes = new System.IO.DirectoryInfo("Themes").GetFiles();
if (Files == null)
Files = new ObservableCollection<FileInfo>();
foreach (var item in localthemes)
{
Files.Add(item);
}
SelectedTheme = Files[0];
}));
this.DataContext = this;
}
public event PropertyChangedEventHandler PropertyChanged;
public void OnChanged(string name)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(name));
}
XAML:
<Window x:Class="WPFTheme.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Name="Window"
Title="MainWindow"
Width="640"
Height="480">
<Grid x:Name="LayoutRoot" Background="{DynamicResource DisabledForegroundBrush}">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="0.285*" />
<ColumnDefinition Width="0.365*" />
<ColumnDefinition Width="0.35*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="0.132*" />
<RowDefinition Height="0.162*" />
<RowDefinition Height="0.403*" />
<RowDefinition Height="0.168*" />
<RowDefinition Height="0.135*" />
</Grid.RowDefinitions>
<Button Width="57"
Margin="15,13,0,10.872"
HorizontalAlignment="Left"
Content="Enabled" />
<Button Width="72"
Margin="0,14,17.12,10.872"
HorizontalAlignment="Right"
Content="Disabled"
IsEnabled="False" />
<TextBlock Grid.Column="1"
Width="69"
Margin="11.88,15,0,27.872"
HorizontalAlignment="Left"
Text="TextBlock"
TextWrapping="Wrap" />
<TextBox Grid.Column="1"
Width="64"
Height="21"
Margin="9.88,0,0,4.872"
HorizontalAlignment="Left"
VerticalAlignment="Bottom"
Text="TextBox"
TextWrapping="Wrap" />
<TextBox Grid.Column="1"
Height="21"
Margin="88.88,0,35.8,3.872"
VerticalAlignment="Bottom"
IsEnabled="False"
Text="TextBox Disabled"
TextWrapping="Wrap" />
<CheckBox Grid.Row="1"
Width="71"
Height="14"
Margin="11,7.128,0,0"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Content="CheckBox" />
<CheckBox Grid.Row="1"
Width="71"
Height="14"
Margin="0,8.128,15.12,0"
HorizontalAlignment="Right"
VerticalAlignment="Top"
Content="Disabled"
IsEnabled="False" />
<ComboBox Grid.Column="2"
Width="94"
Margin="8.2,18,0,11.872"
HorizontalAlignment="Left"
ItemsSource="{Binding Files}"
SelectedItem="{Binding SelectedTheme,
Mode=TwoWay,
UpdateSourceTrigger=PropertyChanged}" />
<ComboBox Grid.Column="2"
Width="94"
Margin="0,17,14,12.872"
HorizontalAlignment="Right"
IsEnabled="False"
ItemsSource="{Binding Files}" />
<DataGrid Grid.Row="2"
Grid.Column="1"
Margin="8.88,6.876,7.8,62.862"
AutoGenerateColumns="True"
ItemsSource="{Binding Files}" />
<DatePicker Grid.Row="2"
Height="23"
Margin="10,0,15,147"
VerticalAlignment="Bottom" />
<GroupBox Grid.Row="2"
Grid.Column="2"
Margin="6.2,2.876,6,5.862"
Header="GroupBox">
<ScrollViewer Margin="6,0.723,1,1" ScrollViewer.HorizontalScrollBarVisibility="Visible">
<ListBox Width="161"
Height="108"
ItemsSource="{Binding Files}" />
</ScrollViewer>
</GroupBox>
<ListView Grid.Row="2"
Grid.Column="1"
Height="59"
Margin="12.88,0,5.8,-4.138"
VerticalAlignment="Bottom"
ItemsSource="{Binding Files}">
<ListView.View>
<GridView>
<GridViewColumn Header="File Name">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
<ProgressBar x:Name="progressBar"
Grid.Row="1"
Grid.Column="1"
Height="20"
Margin="5.88,6.128,61.8,0"
VerticalAlignment="Top"
Value="50" />
<RadioButton Grid.Row="1"
Width="64"
Margin="11,25.128,0,29.124"
HorizontalAlignment="Left"
Content="RadioButton" />
<RadioButton Grid.Row="1"
Width="51"
Margin="0,25.128,33.12,29.124"
HorizontalAlignment="Right"
Content="RadioButton"
IsEnabled="False" />
<Slider Grid.Row="1"
Grid.Column="1"
Margin="11.88,34.128,38.8,15.124"
AutoToolTipPlacement="BottomRight"
Maximum="{Binding Maximum,
ElementName=progressBar}"
Minimum="{Binding Minimum,
ElementName=progressBar}"
Value="{Binding Value,
ElementName=progressBar}" />
<TabControl Grid.Row="1"
Grid.Column="2"
Margin="7.2,9.128,9,0.124">
<TabItem Header="TabItem">
<Grid Background="#FFE5E5E5" />
</TabItem>
<TabItem Header="TabItem">
<Grid Background="#FFE5E5E5" />
</TabItem>
</TabControl>
<TreeView Grid.Row="3"
Margin="8,5.138,12.12,1.79"
ItemsSource="{Binding Files}" />
<ToolBar Grid.Row="4"
Grid.ColumnSpan="2"
Margin="10,9.21,104.8,17">
<Button />
<CheckBox />
<ComboBoxItem />
<MenuItem />
<Separator />
<TabItem />
</ToolBar>
</Grid>
</Window>
I handle Theme switching at start-up in my application like this.
Application.Current.Resources.MergedDictionaries.Clear();
Application.Current.Resources.MergedDictionaries.Add(Themes.Where(p => p.Value.ThemeName == "MyTheme").SingleOrDefault().Value.Theme);
I first clear the Dictionaries to remove any preset Theme. I do this as I use a default theme in the editor, and then during run-time switch depending on the users configuration.
I restart the application to load the new theme, but as you save the states etc in your ViewModel you should be able to reload the UI without having to completely restart the application. This was however not an requirement for my project, so I never went that far.
You could probably just pass on the name of your theme from the View, and then parse it using logic from your ViewModel.
Your problem is that you are trying to change the View directly from your ViewModel, which is not allowed. You need to come up with a more passive solution based on property bindings.
My approach would be have a small piece of code the your main view's code-behind that switches resource files in your merged dictionaries, and the way it does this can be disctated by the value of a property in your ViewModel that it is bound to. A small amount of code-behind to support View-centric behaviour is allowed in MVVM.
Related
I have a tabcontrol with 3 tabs on it. Each tabitem contains a different contentcontrol, all of which are bound to the same ViewModel, and there are some items in all 3 contentcontrols that are bound to the same property.
My problem is, if I change a comboBox on the initial tab, that change is not reflected on the other tabs, they still have their SelectedItem set to the default value. This only happens the first time I switch to that tab. After I have selected a tab once, everything works as expected; changes made on one tab are reflected on the other tab.
I am trying to avoid too much code-behind, and I have searched and tried all of the solutions regarding delaying of the binding, or updating the binding when the tab is selected, but I have had no luck.
Here is my TabControl:
<TabControl Grid.Row="1" Margin="0,10">
<TabItem Header="Tab1" IsSelected="True">
<ContentControl Content="{Binding}" ContentTemplate="{StaticResource Tab1}" />
</TabItem>
<TabItem Header="Tab2">
<ContentControl Content="{Binding}" ContentTemplate="{StaticResource Tab2}" />
</TabItem>
<TabItem Header="Tab3">
<ContentControl Content="{Binding}" ContentTemplate="{StaticResource Tab}" />
</TabItem>
</TabControl>
And here is one of the ContentControls:
<DataTemplate x:Key="Tab3" DataType="{x:Type vm:MainViewModel}">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Label Grid.Column="0" Grid.Row="0" Content="Flow" Style="{StaticResource tabLabel}" />
<TextBox Grid.Column="1" Grid.Row="0" Style="{StaticResource tabTextBox}" />
<ComboBox Grid.Column="2" Grid.Row="0" Style="{StaticResource tabUnitComboBox}" ItemsSource="{Binding DataList1}" DisplayMemberPath="Name" SelectedItem="{Binding SelectedData1}" />
<ComboBox Grid.Column="0" Grid.Row="1" Style="{StaticResource tabComboBox}" ItemsSource="{Binding DataList2}" DisplayMemberPath="Name" SelectedItem="{Binding SelectedData2}" />
<TextBox Grid.Column="1" Grid.Row="1" Style="{StaticResource tabTextBox}" />
<Label Grid.Column="2" Grid.Row="1" Content="m³/h" Style="{StaticResource tabUnitLabel}" Visibility="{Binding SelectedData2, Converter={StaticResource VisibilityConverter}}" />
<ComboBox Grid.Column="0" Grid.Row="2" Style="{StaticResource tabComboBox}" ItemsSource="{Binding DataList3}" SelectedItem="{Binding SelectedData3}" />
<TextBox Grid.Column="1" Grid.Row="2" Style="{StaticResource tabTextBox}" />
<ComboBox Grid.Column="2" Grid.Row="2" Style="{StaticResource tabUnitComboBox}" ItemsSource="{Binding DataList4}" DisplayMemberPath="Name" SelectedItem="{Binding SelectedData4}" />
<Label Grid.Column="0" Grid.Row="3" Content="Temperature" Style="{StaticResource tabLabel}" />
<TextBox Grid.Column="1" Grid.Row="3" Style="{StaticResource tabTextBox}" />
<ComboBox Grid.Column="2" Grid.Row="3" Style="{StaticResource tabUnitComboBox}" ItemsSource="{Binding DataList5}" DisplayMemberPath="Name" SelectedItem="{Binding SelectedData5}" />
</Grid>
</DataTemplate>
The ViewModel is pretty much standard, inheriting from ObservableObject and implementing OnPropertyChanged. As I said, everything works perfectly after selecting a tab once, but I would really like to get it working from the start.
Any suggestions would be greatly appreciated. Thanks.
In case you want to use that comment as and answer
Are you by any chance just starting with assigning the backing field.
Not firing INPC on the initial load.
I have a Groupbox in which i have multiple Textboxes. All these Textbox derive their Datacontext from that of Groupbox but one of the Textbox in the group needs a different Datacontext.
<GroupBox Header="My Group" Height="150" Width="1132" DataContext="{Binding ContextA}" >
<Grid>
<Label x:Name="lblA" Content="Policy Number:" Margin="6,12,970,92" />
<TextBox x:Name="txtbA" Margin="155,12,0,0" HorizontalAlignment="Left" Height="24" TextWrapping="Wrap" Text="{Binding ValueA}" VerticalAlignment="Top" Width="278" Grid.ColumnSpan="2"/>
<Label x:Name="lblB" Content="Policy Type:" Margin="612,10,334,88" Height="30"/>
<TextBox x:Name="txtbB" Margin="801,12,0,0" HorizontalAlignment="Left" Height="24" TextWrapping="Wrap" DataContext="{Binding ContextB}" Text="{Binding ValueB}" VerticalAlignment="Top" Width="278"/>
</Grid>
</GroupBox>
In the above code txtbA uses the Datacontext same as that of Groupbox.
I want txtbB to have a separate Datacontexti.e. ContextB
But the ContextB is not getting assigned to txtbB. How to solve the problem?
Note:
ContextAand ContextB= list of Entity Framework models.
WPF binding engine look for property in current DataContext. So, in your case binding engine is looking for property ContextB in class ContextA since textBox is inheriting DataContext from parent GroupBox.
What you can do is use more verbose definition for ContextA like this:
<GroupBox Header="My Group" Height="150" Width="1132"
DataContext="{Binding}"> <-- HERE Or can remove setting DC altogether.
<Grid>
<Label x:Name="lblA" Content="Policy Number:" Margin="6,12,970,92" />
<TextBox x:Name="txtbA" Margin="155,12,0,0" HorizontalAlignment="Left"
Height="24" TextWrapping="Wrap"
Text="{Binding ContextA.ValueA}" <-- HERE
VerticalAlignment="Top"
Width="278" Grid.ColumnSpan="2"/>
<Label x:Name="lblB" Content="Policy Type:" Margin="612,10,334,88"
Height="30"/>
<TextBox x:Name="txtbB" Margin="801,12,0,0" HorizontalAlignment="Left"
Height="24"
TextWrapping="Wrap" DataContext="{Binding ContextB}"
Text="{Binding ValueB}" VerticalAlignment="Top" Width="278"/>
</Grid>
</GroupBox>
I've got a ListBox with CheckBoxes in it which resizes automatically when I resize the Window.
I found the CheckedListBox XAML code on StackOverflow I think and I didn't modify it, and I'd like the same thing for all the UI elements.
I've read something about setting Anchors. As I already said, I didn't do anything to the XAML code, neither did I set any Anchors?
Is there anything in the XAML code which I'm not seeing which is responsible for the resizing?
<Grid>
<TextBox Height="23" HorizontalAlignment="Left" Margin="12,28,0,0" Name="textBox1" VerticalAlignment="Top" Width="238" />
<Label Content="Profilname" Height="28" HorizontalAlignment="Left" Margin="12,0,0,0" Name="label1" VerticalAlignment="Top" />
<ListBox ItemsSource="{Binding datasource}" Margin="12,57,12,41" Name="checkedListBox1" Opacity="0.7">
<ListBox.ItemTemplate>
<DataTemplate>
<CheckBox Content="{Binding Path=Item}" IsChecked="{Binding IsChecked}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<Button Content="save" Margin="12,302,12,12" Name="button3" Click="button3_Click" />
</Grid>
Thanks in advance!
You are doing it like a Canvas, but you are using a Grid.
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Label Content="Profilname" Name="label1" Grid.Row="0" />
<TextBox Name="textBox1" Grid.Row="1" />
<ListBox ItemsSource="{Binding datasource}" Name="checkedListBox1" Grid.Row="2" >
<ListBox.ItemTemplate>
<DataTemplate>
<CheckBox Content="{Binding Path=Item}" IsChecked="{Binding IsChecked}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<Button Content="save" Name="button3" Click="button3_Click" Grid.Row="3" />
</Grid>
See now everything resizes.
You can add margin and padding now as desired for styling.
I'm trying to make a list that is populated by external data and I have a datatemplate for it.
Each element of the list is composed of an image and two textblocks, and is linked to a specific page.
I'm trying to do this but when I wrap the structure with an HyperlinkButton, I just get a blank page.
I don't know if I'm doing something stupid wrong or it's not possible to have so many items in an HyperlinkButton. If it's not possible to do it this way, can someone guide me to the best solution to do this? Here's the code:
<Grid x:Name="ContentPanel" Margin="0,140,0,0" Background="White">
<ListBox HorizontalAlignment="Stretch" Name="itemList" VerticalContentAlignment="Stretch">
<ListBox.ItemTemplate>
<DataTemplate>
<HyperlinkButton>
<Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch" MinWidth="480">
<Grid.Background>
<ImageBrush ImageSource="/app;component/Images/degradat_cela.png" Stretch="UniformToFill" AlignmentY="Top" AlignmentX="Left" />
</Grid.Background>
<Grid.RowDefinitions>
<RowDefinition Height="35" />
<RowDefinition Height="35" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="50" />
<ColumnDefinition Width="430*" />
</Grid.ColumnDefinitions>
<Border HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Height="38" Width="38" Grid.RowSpan="2" Grid.Column="0" Grid.Row="0" BorderThickness="1" BorderBrush="#FFFF003F" Padding="1">
<Image HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Name="listImage" Width="36" Height="36" Source="{Binding image}" />
</Border>
<TextBlock Margin="5 12 0 0" Grid.Column="1" Grid.Row="0" Name="title" Foreground="Black" Text="{Binding title}" FontWeight="Bold" FontSize="18" />
<TextBlock Margin="5 0 0 8" Grid.Column="1" Grid.Row="1" Name="description" Foreground="Black" Text="{Binding subtitle}" FontSize="14" />
</Grid>
</HyperlinkButton>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
I will also accept any suggestions to make my code better, as I'm new to .NET and I probably don't do things the best way!
Remove the HyperlinkButton and instead use the SelectionChanged event of the ListBox. So add this property to your ListBox:
SelectionChanged="myListBox_SelectionChanged"
Then in your code behind do this:
private void myListBox_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
if ((sender as ListBox).SelectedIndex == -1)
return;
NavigationService.Navigate(new System.Uri(string.Format("/Drilldown.xaml?Index={0}",(sender as ListBox).SelectedIndex),System.UriKind.Relative));
}
This code assumes a drilldown page that uses a query string to change it's layout. This is just for example. If instead of index you wanted to reference some property of the bound item you could instead do something like this (assuming an ID property):
int itemID = ((sender as ListBox).SelectedItem as MyApp.Model.myItem).ID;
NavigationService.Navigate(new System.Uri(string.Format("/Drilldown.xaml?ID={0}",itemID),System.UriKind.Relative));
Inside the pivot control, I have a DataTemplate (TestItemTemplate) for the ItemTemplate. The DataContext for the page is set to {Binding RelativeSource={RelativeSource Self}} and the ItemsSource for PivotControl is bound to an observable collection.
Inside the DataTemplate of the pivotcontrol, I have a ListPicker which I want to bind to IEnumerable. I have created a public property of
IEnumerable TestEntries = "One Two Three".Split();
The listpicker doesn't show any bound items though. If I place the listpicker outside the data template (as a sibling of the PivotControl, it shows the three strings in the picker)
<phone:PhoneApplicationPage.Resources>
<DataTemplate x:Key="TestItemTemplate">
<Grid Margin="0,-25,0,0">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" x:Name="AnotherContainer" />
<RowDefinition Height="300" x:Name="TestDescriptionContainer" />
<RowDefinition Height="Auto" x:Name="SaveCancelDeleteContainer" />
</Grid.RowDefinitions>
<toolkit:ListPicker x:Name="lstPicker" Grid.Row="0" ItemsSource="{Binding TestEntries}" Header="situation" FullModeHeader="SITUATIONS">
<toolkit:ListPicker.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding}" Margin="4 0 0 0"/>
</StackPanel>
</DataTemplate>
</toolkit:ListPicker.ItemTemplate>
<toolkit:ListPicker.FullModeItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Margin="16 21 0 20">
<TextBlock Text="{Binding}" Margin="4 0 0 0" FontSize="43" FontFamily="{StaticResource PhoneFontFamilyLight}"/>
</StackPanel>
</DataTemplate>
</toolkit:ListPicker.FullModeItemTemplate>
</toolkit:ListPicker>
<TextBox Grid.Row="1" Text="{Binding Description}" TextWrapping="Wrap" VerticalAlignment="Top" d:LayoutOverrides="Width" AcceptsReturn="True" Height="300"/>
<StackPanel Grid.Row="2" Orientation="Horizontal" HorizontalAlignment="Center" Margin="0,2,0,0" >
<Button x:Name="SaveButton" Content="Save" Margin="5" Click="SaveButton_Click" Width="140" />
<Button x:Name="CancelButton" Content="Cancel" Margin="5" Click="CancelButton_Click" Width="140" />
<Button x:Name="DeleteButton" Content="Delete" Margin="5" Click="DeleteButton_Click" Width="140" />
</StackPanel>
</Grid>
</DataTemplate>
</phone:PhoneApplicationPage.Resources>
<!--LayoutRoot is the root grid where all page content is placed-->
<Grid x:Name="LayoutRoot" Background="Transparent">
<!--Pivot Control-->
<controls:Pivot
x:Name="PivotControl"
Title="{StaticResource AppName}"
ItemsSource="{Binding TestEntries}"
ItemTemplate="{StaticResource TestItemTemplate}"
SelectionChanged="PivotControl_SelectionChanged"
>
</controls:Pivot>
</Grid>
I figured this out on my own. Here is the solution if others run into the same issue.
I think this is required to set the DataContext properly when the ListPicker is inside a DataTemplate because the in Page's initialize method or the loaded event handler, the ListPicker inside the DataTemplate is still null. Use the control's own Loaded event handler to initialize it.
I had to Set the DataContext of the ListPicker inside its own Loaded eventhandler. Something like this:
private void lstTestEntriesPicker_Loaded(object sender, RoutedEventArgs e)
{
ListPicker lstTestEntriesPicker= VisualElementHelper.FindName<ListPicker>("lstTestEntriesPicker", this);
if (lstTestEntriesPicker!= null)
{
lstTestEntriesPicker.DataContext = TestEntries;
}
}
XAML looks like this:
<toolkit:ListPicker x:Name="lstTestEntriesPicker" ItemsSource="{Binding}" Grid.Row="0" Header="TestEntries" FullModeHeader="TestEntries" Loaded="lstTestEntriesPicker_Loaded">
<toolkit:ListPicker.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding}" Margin="4 0 0 0"/>
</StackPanel>
</DataTemplate>
</toolkit:ListPicker.ItemTemplate>
<toolkit:ListPicker.FullModeItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Margin="16 21 0 20">
<TextBlock Text="{Binding}" Margin="4 0 0 0" FontSize="43" FontFamily="{StaticResource PhoneFontFamilyLight}"/>
</StackPanel>
</DataTemplate>
</toolkit:ListPicker.FullModeItemTemplate>
</toolkit:ListPicker>