Why is Binding not updating - c#

The Background
The summary of what I have is a UserControl MarkdownEditor. It contains a TextBox which has display properties like FontFamily, Background etc controlled by Bindings. It gets these values from an MarkdownEditorOptions class, which contains properties for each of these values. My code looks like below
In ShellView, showing how I might set display options for the TextBox in my MarkdownEditor
<me:MarkdownEditor>
<me:MarkdownEditor.Options>
<me:MarkdownEditorOptions Background="Red" />
</me:MarkdownEditor.Options>
</me:MarkdownEditor>
In MarkdownEditor.xaml.cs, DataContext for MarkdownEditor (UserControl), the declaration for Options
public MarkdownEditorOptions Options
{
get { return (MarkdownEditorOptions)GetValue(OptionsProperty); }
set { SetValue(OptionsProperty, value); }
}
public static readonly DependencyProperty OptionsProperty =
DependencyProperty.Register("Options", typeof(MarkdownEditorOptions), typeof(MarkdownEditor), new UIPropertyMetadata(new MarkdownEditorOptions()));
In MarkdownEditor.xaml : Showing the way TextBox binds to Option values
<TextBox Grid.Row="1" x:Name="txtEditor" AcceptsReturn="True" Text="{Binding Path=Content, UpdateSourceTrigger=PropertyChanged}"
FontFamily="{Binding RelativeSource={RelativeSource AncestorType={x:Type local:MarkdownEditor}}, Path=Options.FontFamily}"
FontSize="{Binding RelativeSource={RelativeSource AncestorType={x:Type local:MarkdownEditor}}, Path=Options.FontSize}"
FontWeight="{Binding RelativeSource={RelativeSource AncestorType={x:Type local:MarkdownEditor}}, Path=Options.FontWeight}"
Background="{Binding RelativeSource={RelativeSource AncestorType={x:Type local:MarkdownEditor}}, Path=Options.Background, Converter={StaticResource colorToBrushConverter}}"
Foreground="{Binding RelativeSource={RelativeSource AncestorType={x:Type local:MarkdownEditor}}, Path=Options.Foreground, Converter={StaticResource colorToBrushConverter}}" />
The MarkdownEditorOptions class
// MarkdownEditorOptions
public class MarkdownEditorOptions : ObservableObject
{
protected FontFamily _fontFamily;
protected int _fontSize;
protected FontWeight _fontWeight;
protected Color _background;
protected Color _foreground;
// Constructor, for default options
public MarkdownEditorOptions()
{
_fontFamily = new FontFamily("Consolas");
_fontSize = 14;
_fontWeight = FontWeights.Bold;
_background = new Color { R = 32, G = 32, B = 32, A = 255 };
_foreground = new Color { R = 255, G = 255, B = 255, A = 255 };
}
public FontFamily FontFamily {
get { return _fontFamily; }
set {
_fontFamily = value;
RaisePropertyChanged("FontFamily");
}
}
public int FontSize
{
get { return _fontSize; }
set {
_fontSize = value;
RaisePropertyChanged("FontSize");
}
}
public FontWeight FontWeight
{
get { return _fontWeight; }
set {
_fontWeight = value;
RaisePropertyChanged("FontWeight");
}
}
public Color Background
{
get { return _background; }
set {
_background = value;
RaisePropertyChanged("Background");
}
}
public Color Foreground
{
get { return _foreground; }
set {
_foreground = value;
RaisePropertyChanged("Foreground");
}
}
}
The Problem
My TextBox in MarkdownEditor is always showing the defaults from constructor of MarkdownEditorOptions. In the simple XAML I've shown, the red background does not seem to be applied. Whats wrong?
[Update: 17 Nov: 4:25PM]
A Few Thoughts
I am thinking it has something to do with Path=Options.FontSize. Maybe this binding will track changes to Options instead of Options.FontSize?
UPDATE: 19 Nov
A few observations: If I use the control in a separate simple window
<Window ...>
<Window.Resources>
<me:MarkdownEditorOptions FontFamily="Arial" FontWeight="Normal" Background="Red" x:Key="options" />
</Window.Resources>
<Grid>
<Grid.RowDefinitions ... />
<Button Content="Options ..." Grid.Row="0" Click="Button_Click" />
<me:MarkdownEditor Grid.Row="1" Options="{StaticResource options}" x:Name="markdownEditor" />
</Grid>
</Window>
Things works fine, if I use it in a more complex setup. TabControl bound to ObservableCollection<TabViewModel>, it fails
<TabControl ... ItemsSource="{Binding TabsViewSource}" IsSynchronizedWithCurrentItem="True">
I tried with and without bindings. It seems the setters in MarkdownEditorOptions is run, as I added Debug.WriteLine() but the background etc does not update.
<DataTemplate DataType="{x:Type vm:EditorTabViewModel}">
<!--<me:MarkdownEditor Options="{Binding RelativeSource={RelativeSource AncestorType={x:Type v:ShellView}}, Path=ViewModel.Options}" />-->
<me:MarkdownEditor>
<me:MarkdownEditor.Options>
<me:MarkdownEditorOptions Background="Red" />
</me:MarkdownEditor.Options>
</me:MarkdownEditor>
</DataTemplate>

Usually this is due to incorrect Path. Unfortunately, it will not raise an exception, you can try attaching vs debugger to your application and check for any binding error in debug log

This was fixed in another question of mine here on StackOverflow: Binding Setting Property but UI not updating. Can I debug within referenced project/control?

Related

Image Opacity, turn of and on

I have five images, when you click one of them I want that one to get full opacity while the other only gets half, to show it is the selected one.
I am using MVVM and generally wondering if I'm doing it the right way
I was thinking about passing the name of the imagesource binded into a property.
<StackLayout Grid.Row="3" Grid.Column="1" Orientation="Horizontal" Spacing="0">
<Image Source="{Binding StatusUnresolved}" HorizontalOptions="Center"
VerticalOptions="Center" HeightRequest="40" Opacity="{Binding StatusUnresolvedOpacity}">
<Image.GestureRecognizers>
<!--<TapGestureRecognizer Command="{Binding Source={x:Reference this}, Path=OnStatusTappedCommand}" CommandParameter="{Binding StatusUnresolved}" />-->
</Image.GestureRecognizers>
</Image>
</StackLayout>
The list that turns the string into status later on.
public List<IssueStatusModel> PossibleStatusValues
{
get
{
var items = new List<IssueStatusModel>
{
new IssueStatusModel("statusUnresolved.png", IssueStatus.Unresolved),
new IssueStatusModel("statusInProgress.png", IssueStatus.InProgress),
new IssueStatusModel("statusDone.png", IssueStatus.Done)
};
return items;
}
}
Property for opacity
public double StatusDoneOpacity
{
get { return statusDoneOpacity; }
set
{
if (statusDoneOpacity != value)
{
statusDoneOpacity = value;
NotifyPropertyChanged(nameof(StatusUnresolvedOpacity));
}
}
}
public string StatusDone
{
get { return "statusDone.png"; }
}
public void OnStatusTapped(string fileName)
{
foreach (IssueStatusModel item in StatusValues)
{
if (item.Name != fileName) continue;
Issue.Status = item.Status;
StatusChecker();
return;
}
}
}
Switch statement Changing all the opacities.
private void StatusChecker()
{
switch (Issue.Status)
{
case IssueStatus.Unresolved:
StatusUnresolvedOpacity = 1;
StatusInProgressOpacity = 0.5;
StatusDoneOpacity = 0.5;
StatusText = "Unresolved";
break;
case IssueStatus.InProgress:
StatusUnresolvedOpacity = 0.5;
StatusInProgressOpacity = 1;
StatusDoneOpacity = 0.5;
StatusText = "In Progress";
break;
case IssueStatus.Done:
StatusUnresolvedOpacity = 0.5;
StatusInProgressOpacity = 0.5;
statusDoneOpacity = 1;
StatusText = "Done";
break;
}
}
The way I'd attack this, if you have multiple images, create an ImageVm and encapsulate any image specific implementation details i.e. enum State and an IsSelected notification properties. Of course if you only have 1 image this becomes trivially easy and you don't need vms
Use a DataTrigger that binds to an IsSelected MVVM property to set the Opacity and state if you need to change the image source. Obviously on click you will need to set the IsSelected Property and deselect the other VMs
Example of DataTrigger for IsSelected
<Image Grid.Column="2" Stretch="None">
<Image.Style>
<Style TargetType="{x:Type Image}">
<Setter Property="Opacity" Value="0.5" />
<Style.Triggers>
<DataTrigger Value="True" Binding="{Binding IsSelected}">
<Setter Property="Opacity" Value="0.5"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Image.Style>
</Image>
Update
You CAN use triggers with enums, and you can use a tap recognizers to fire commands in your main viewmodals. also commands can take parameters as well.
It's probably better (knowing what you have described in the comments) to just make a State and Severity enum and bind to it, and set the State and Severity via a command by a gesture.
Then you could just make a Trigger for each Image to change the Opacity for each image on the various state and severity.

TextBlock with text highlighting

Faced the need to select a fragment of text in TextBlock, namely certain keywords on which the ListBox was filtered, this text block itself and containing
XAML variant, title property is not bound
<ListBox Name="ProcedureList" ItemsSource="{Binding Path=ProceduresView.View}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Name="ProcedurePanel" PreviewMouseDown="ProcedurePanel_OnPreviewMouseDown">
<DockPanel Width="{c:Binding ElementName=MainPanel, Path=Width-40}">
<!--<TextBlock Name="MainText" TextWrapping="Wrap" FontSize="16" Text="{Binding Path=title}" HorizontalAlignment="Left" />-->
<htb:HighlightTextBlock Name="MainText" TextWrapping="Wrap" FontSize="16" Text="{Binding Path=title}" HorizontalAlignment="Left">
<htb:HighlightTextBlock.HighlightRules>
<htb:HighlightRule
IgnoreCase="{Binding IgnoreCase, Source={StaticResource SourceVm}}"
HightlightedText="{Binding Path=title, Converter={StaticResource getFilter}}">
<htb:HighlightRule.Highlights>
<htb:HighlightBackgroung Brush="Yellow"/>
</htb:HighlightRule.Highlights>
</htb:HighlightRule>
</htb:HighlightTextBlock.HighlightRules>
</htb:HighlightTextBlock>
</DockPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
A component written by our compatriot with open source is used
Component
Description of component
The commented code is an old TexBlock with no selection
The new HighlightTextBlock component perfectly selects the text if you use a static resource, as in the example, but when I try to bind it to the current text it can not find this field :(, I'm new in WPF help figure it out
HightlightedText="{Binding Path=title, Converter={StaticResource getFilter}}"
How correctly to anchor this property to title?
DataContext structure
public ObservableCollection<Procedure> Procedures { set; get; }
public CollectionViewSource ProceduresView { set; get; } = new CollectionViewSource();
....
Procedures = new ObservableCollection<Procedure>();
ProceduresView.Filter += Procedures_Filter;
ProceduresView.Source = Procedures;
....
public class Procedure : ObservableObject
{
....
public String title { get; set; }
....
}
....
// Simple filtering
void Procedures_Filter(object sender, FilterEventArgs e)
{
Procedure procedure = (Procedure) e.Item;
Boolean flag = false;
if (!string.IsNullOrEmpty(filter))
{
Setting.Filter sfilter = new Setting.Filter();
sfilter.type = "искать везде";
sfilter.text = filter;
ObservableCollection<Setting.Filter> arr = new ObservableCollection<Setting.Filter>();
arr.Add(sfilter);
if (Utils.AssignedProcedureFromFilter(procedure, arr)) flag = true;
}
else flag = true;
e.Accepted = flag;
}
Video with problem description
Simplified project emitting my functional
On the Russian-speaking forum they explained to me that:
Your case, in fact, is more serious. DataContext you, apparently, the
right one. But your Binding expression is inside the HighlightRules
property setter, which is not part of the visual tree (because it is
not available as a Child element of your control). And elements that
are not inside the visual tree, participate in bindings are only
limited: they do not inherit DataContext, nor access by name through
ElementName. As a solution, bind to an element via x: Reference. In my
(heavily cut) test case, HightlightedText = "{Binding Path =
DataContext.title, Source = {x: Reference MainText}} is triggered."
But, if directly replaced by this, a strange error works: 'Can not
call MarkupExtension. ProvideValue because of a cyclic dependency. The
properties inside the MarkupExtension can not reference objects that
reference the MarkupExtension result.
The workaround for the error was found here: you need to put your element in resources. We get this:
XAML, modified according to the recommendations
<ListBox Name="ProcedureList" ItemsSource="{Binding Path=ProceduresView.View}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Name="ProcedurePanel" PreviewMouseDown="ProcedurePanel_OnPreviewMouseDown">
<DockPanel Width="{c:Binding ElementName=MainPanel, Path=Width-40}">
<!--<TextBlock Name="MainText" TextWrapping="Wrap" FontSize="16" Text="{Binding Path=title}" HorizontalAlignment="Left" />-->
<htb:HighlightTextBlock Name="MainText" TextWrapping="Wrap" FontSize="16"
Text="{Binding Path=title}" HorizontalAlignment="Left">
<htb:HighlightTextBlock.Resources>
<htb:HighlightRule x:Key="HR"
IgnoreCase="{Binding IgnoreCase, Source={StaticResource SourceVm}}"
HightlightedText="{Binding Path=DataContext.title, Source={x:Reference MainText}, Converter={StaticResource getFilter}}">
<htb:HighlightRule.Highlights>
<htb:HighlightBackgroung Brush="Yellow"/>
</htb:HighlightRule.Highlights>
</htb:HighlightRule>
</htb:HighlightTextBlock.Resources>
<htb:HighlightTextBlock.HighlightRules>
<htb:HighlightRulesCollection>
<StaticResource ResourceKey="HR"/>
</htb:HighlightRulesCollection>
</htb:HighlightTextBlock.HighlightRules>
</htb:HighlightTextBlock>
</DockPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
I was given advice on the restructuring of XAML, through resources, this partially solved the problem (I successfully got the title text in the converter), but the element ceased to perform its functions (allocation) During the discussion, it was suggested that the component itself should be finalized
#iRumba: In theory, the whole trick should not be necessary if you put
the HighlighRule collection (also) in a visual tree. Then the
DataContext will be automatically inherited and on idea the binding
through ElementName too will work.
#iRumba: I do not remember exactly. It seems, it is necessary to
specify to add all HighlightRule as LogicalChildren (for this purpose
on idea it is necessary to redefine protected internal override
IEnumerator LogicalChildren). This is a complicated, advanced
technique, yes.
Sorry for Google Translator
Found a solution
public class SearchHightlightTextBlock : TextBlock
{
public SearchHightlightTextBlock() : base() { }
public String SearchText
{
get { return (String)GetValue(SearchTextProperty); }
set { SetValue(SearchTextProperty, value); }
}
private static void OnDataChanged(DependencyObject source,
DependencyPropertyChangedEventArgs e)
{
TextBlock tb = (TextBlock)source;
if (tb.Text.Length == 0)
return;
string textUpper = tb.Text.ToUpper();
String toFind = ((String)e.NewValue).ToUpper();
int firstIndex = textUpper.IndexOf(toFind);
String firstStr = "";
String foundStr = "";
if (firstIndex != -1)
{
firstStr = tb.Text.Substring(0, firstIndex);
foundStr = tb.Text.Substring(firstIndex, toFind.Length);
}
String endStr = tb.Text.Substring(firstIndex + toFind.Length,
tb.Text.Length - (firstIndex + toFind.Length));
tb.Inlines.Clear();
tb.FontSize = 16;
var run = new Run();
run.Text = firstStr;
tb.Inlines.Add(run);
run = new Run();
run.Background = Brushes.Yellow;
run.Text = foundStr;
tb.Inlines.Add(run);
run = new Run();
run.Text = endStr;
tb.Inlines.Add(run);
}
public static readonly DependencyProperty SearchTextProperty =
DependencyProperty.Register("SearchText",
typeof(String),
typeof(SearchHightlightTextBlock),
new FrameworkPropertyMetadata(null, OnDataChanged));
}
Use
<parser:SearchHightlightTextBlock SearchText="{Binding Path=title, Converter={StaticResource getFilter}}" Text="{Binding title}"/>

Binding all System Colors to a ListBox

I'd like to bind all Windows.UI.Colors to a ListBox (ListView?) in a XAML Page, in a Universal Windows App (for Windows 10, in Visual Studio 2015).
I found this way to get all system colors:
Dictionary<string, Windows.UI.Color> Colors()
{
var _Colors = typeof(Windows.UI.Colors)
// using System.Reflection;
.GetRuntimeProperties()
.Select(c => new
{
Color = (Windows.UI.Color)c.GetValue(null),
Name = c.Name
});
return _Colors.ToDictionary(x => x.Name, x => x.Color);
}
I don't know how to bind it to a ListBox
<ListBox ItemsSource="{x:Bind colors}" >
</ListBox>
Ideally the list item text should be the color name, and the list item background the color value.
An alternative approach to #Romasz's answer:
Change the Color() method to a property, and return a dictionary with SolidColorBrush values instead of Color like so:
public Dictionary<string, SolidColorBrush> Colors
{
get
{
var _Colors = typeof(Windows.UI.Colors)
// using System.Reflection;
.GetRuntimeProperties()
.Select(c => new
{
Color = new SolidColorBrush((Windows.UI.Color)c.GetValue(null)),
Name = c.Name
});
return _Colors.ToDictionary(x => x.Name, x => x.Color);
}
}
Then, in the XAML, change the list box to this:
<ListBox ItemsSource="{x:Bind Colors}" >
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Background="{Binding Value}">
<TextBlock Text="{Binding Key}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
There is couple of things you need to improve about binding (and I would advise to read some more at MSDN). As for your code - in xaml you will need to declare how your ItemTemplate will look like and bind to a property within DataContext. You will also need probably a converter to convert Color to Brush:
<StackPanel Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<ListView ItemsSource="{Binding MyColors}">
<ListView.Resources>
<local:ColorToBrush x:Key="ColorToBrush"/>
</ListView.Resources>
<ListView.ItemTemplate>
<DataTemplate>
<Border Background="{Binding Color, Converter={StaticResource ColorToBrush}}">
<TextBlock Text="{Binding Name}"/>
</Border>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackPanel>
And the code behind - converter class, the suitable property and setting DataContext in constructor:
public class ColorToBrush : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string language) => new SolidColorBrush((Windows.UI.Color)value);
public object ConvertBack(object value, Type targetType, object parameter, string language) { throw new NotImplementedException(); }
}
public sealed partial class MainPage : Page
{
// this is the shortcut of {get { return ... }}
public Array MyColors => typeof(Windows.UI.Colors).GetRuntimeProperties()
.Select(c => new
{
Color = (Windows.UI.Color)c.GetValue(null),
Name = c.Name
}).ToArray();
public MainPage()
{
this.InitializeComponent();
DataContext = this;
}
}
Of course you can also bind to Dictionary, then you will have to exchange in XAML bindings: Name -> Key and Color -> Value.
public MainPage()
{
this.InitializeComponent();
this.Loaded += MainPage_Loaded;
}
private void MainPage_Loaded(object sender, RoutedEventArgs e)
{
List<SystemColor> legacyBrushes = new List<SystemColor>();
foreach(var b in this.LegacyBrushes)
{
SystemColor c = new SystemColor();
c.ColorName = b;
c.ColorBrush = Application.Current.Resources[b] as SolidColorBrush;
legacyBrushes.Add(c);
}
foreach (var b in this.SystemBrushes)
{
SystemColor c = new SystemColor();
c.ColorName = b;
c.ColorBrush = Application.Current.Resources[b] as SolidColorBrush;
legacyBrushes.Add(c);
}
this._lvColors.ItemsSource = legacyBrushes;
}
public class SystemColor
{
public string ColorName { get; set; }
public SolidColorBrush ColorBrush { get; set; }
}
public string[] LegacyBrushes = new string[] {
"AppBarBackgroundThemeBrush",
"AppBarBorderThemeBrush",
"AppBarItemBackgroundThemeBrush",
"AppBarItemDisabledForegroundThemeBrush",
"AppBarItemForegroundThemeBrush",
"AppBarItemPointerOverBackgroundThemeBrush",
"AppBarItemPointerOverForegroundThemeBrush",
"AppBarItemPressedForegroundThemeBrush",
"AppBarSeparatorForegroundThemeBrush",
"AppBarToggleButtonCheckedBackgroundThemeBrush",
"AppBarToggleButtonCheckedBorderThemeBrush",
"AppBarToggleButtonCheckedDisabledBackgroundThemeBrush",
"AppBarToggleButtonCheckedDisabledBorderThemeBrush",
"AppBarToggleButtonCheckedDisabledForegroundThemeBrush",
"AppBarToggleButtonCheckedForegroundThemeBrush",
"AppBarToggleButtonCheckedPointerOverBackgroundThemeBrush",
"AppBarToggleButtonCheckedPointerOverBorderThemeBrush",
"AppBarToggleButtonCheckedPressedBackgroundThemeBrush",
"AppBarToggleButtonCheckedPressedBorderThemeBrush",
"AppBarToggleButtonCheckedPressedForegroundThemeBrush",
"AppBarToggleButtonPointerOverBackgroundThemeBrush",
"ApplicationForegroundThemeBrush",
"ApplicationHeaderForegroundThemeBrush",
"ApplicationPageBackgroundThemeBrush",
"ApplicationPointerOverForegroundThemeBrush",
"ApplicationPressedForegroundThemeBrush",
"ApplicationSecondaryForegroundThemeBrush",
"AutoSuggestBackgroundThemeBrush",
"BackButtonBackgroundThemeBrush",
"BackButtonDisabledForegroundThemeBrush",
"BackButtonForegroundThemeBrush",
"BackButtonPointerOverBackgroundThemeBrush",
"BackButtonPointerOverForegroundThemeBrush",
"BackButtonPressedForegroundThemeBrush",
"ButtonBackgroundThemeBrush",
"ButtonBorderThemeBrush",
"ButtonDisabledBackgroundThemeBrush",
"ButtonDisabledBorderThemeBrush",
"ButtonDisabledForegroundThemeBrush",
"ButtonForegroundThemeBrush",
"ButtonPointerOverBackgroundThemeBrush",
"ButtonPointerOverForegroundThemeBrush",
"ButtonPressedBackgroundThemeBrush",
"ButtonPressedForegroundThemeBrush",
"CheckBoxBackgroundThemeBrush",
"CheckBoxBorderThemeBrush",
"CheckBoxContentDisabledForegroundThemeBrush",
"CheckBoxContentForegroundThemeBrush",
"CheckBoxDisabledBackgroundThemeBrush",
"CheckBoxDisabledBorderThemeBrush",
"CheckBoxDisabledForegroundThemeBrush",
"CheckBoxForegroundThemeBrush",
"CheckBoxPointerOverBackgroundThemeBrush",
"CheckBoxPointerOverBorderThemeBrush",
"CheckBoxPointerOverForegroundThemeBrush",
"CheckBoxPressedBackgroundThemeBrush",
"CheckBoxPressedBorderThemeBrush",
"CheckBoxPressedForegroundThemeBrush",
"ComboBoxArrowDisabledForegroundThemeBrush",
"ComboBoxArrowForegroundThemeBrush",
"ComboBoxArrowPressedForegroundThemeBrush",
"ComboBoxBackgroundThemeBrush",
"ComboBoxBorderThemeBrush",
"ComboBoxDisabledBackgroundThemeBrush",
"ComboBoxDisabledBorderThemeBrush",
"ComboBoxDisabledForegroundThemeBrush",
"ComboBoxFocusedBackgroundThemeBrush",
"ComboBoxFocusedBorderThemeBrush",
"ComboBoxFocusedForegroundThemeBrush",
"ComboBoxForegroundThemeBrush",
"ComboBoxHeaderForegroundThemeBrush",
"ComboBoxItemDisabledForegroundThemeBrush",
"ComboBoxItemPointerOverBackgroundThemeBrush",
"ComboBoxItemPointerOverForegroundThemeBrush",
"ComboBoxItemPressedBackgroundThemeBrush",
"ComboBoxItemPressedForegroundThemeBrush",
"ComboBoxItemSelectedBackgroundThemeBrush",
"ComboBoxItemSelectedDisabledBackgroundThemeBrush",
"ComboBoxItemSelectedDisabledForegroundThemeBrush",
"ComboBoxItemSelectedForegroundThemeBrush",
"ComboBoxItemSelectedPointerOverBackgroundThemeBrush",
"ComboBoxPlaceholderTextForegroundThemeBrush",
"ComboBoxPointerOverBackgroundThemeBrush",
"ComboBoxPointerOverBorderThemeBrush",
"ComboBoxPopupBackgroundThemeBrush",
"ComboBoxPopupBorderThemeBrush",
"ComboBoxPopupForegroundThemeBrush",
"ComboBoxPressedBackgroundThemeBrush",
"ComboBoxPressedBorderThemeBrush",
"ComboBoxPressedHighlightThemeBrush",
"ComboBoxPressedForegroundThemeBrush",
"ComboBoxSelectedBackgroundThemeBrush",
"ComboBoxSelectedPointerOverBackgroundThemeBrush",
"ContentDialogBackgroundThemeBrush",
"ContentDialogBorderThemeBrush",
"ContentDialogContentForegroundBrush",
"ContentDialogDimmingThemeBrush",
"DatePickerHeaderForegroundThemeBrush",
"DatePickerForegroundThemeBrush",
"DefaultTextForegroundThemeBrush",
"FlipViewButtonBackgroundThemeBrush",
"FlipViewButtonBorderThemeBrush",
"FlipViewButtonForegroundThemeBrush",
"FlipViewButtonPointerOverBackgroundThemeBrush",
"FlipViewButtonPointerOverBorderThemeBrush",
"FlipViewButtonPointerOverForegroundThemeBrush",
"FlipViewButtonPressedBackgroundThemeBrush",
"FlipViewButtonPressedBorderThemeBrush",
"FlipViewButtonPressedForegroundThemeBrush",
"FlyoutBackgroundThemeBrush",
"FlyoutBorderThemeBrush",
"FocusVisualBlackStrokeThemeBrush",
"FocusVisualWhiteStrokeThemeBrush",
"HyperlinkButtonBackgroundThemeBrush",
"HyperlinkButtonBorderThemeBrush",
"HyperlinkDisabledThemeBrush",
"HyperlinkForegroundThemeBrush",
"HyperlinkPointerOverForegroundThemeBrush",
"HyperlinkPressedForegroundThemeBrush",
"HubSectionHeaderPointerOverForegroundThemeBrush",
"HubSectionHeaderPressedForegroundThemeBrush",
"IMECandidateBackgroundThemeBrush",
"IMECandidateForegroundThemeBrush",
"IMECandidatePointerOverBackgroundThemeBrush",
"IMECandidatePointerOverForegroundThemeBrush",
"IMECandidateSecondaryForegroundThemeBrush",
"IMECandidateSelectedBackgroundThemeBrush",
"IMECandidateSelectedForegroundThemeBrush",
"IMECandidateListBackgroundThemeBrush",
"IMECandidateListPagingButtonBackgroundThemeBrush",
"IMECandidateListPagingButtonBorderThemeBrush",
"IMECandidateListPagingButtonForegroundThemeBrush",
"IMECandidateListPagingButtonPointerOverBackgroundThemeBrush",
"IMECandidateListPagingButtonPointerOverForegroundThemeBrush",
"IMECandidateListPagingButtonPressedBackgroundThemeBrush",
"IMECandidateListPagingButtonPressedForegroundThemeBrush",
"JumpListDefaultEnabledForeground",
"JumpListDefaultEnabledBackground",
"JumpListDefaultDisabledForeground",
"JumpListDefaultDisabledBackground",
"ListBoxBackgroundThemeBrush",
"ListBoxBorderThemeBrush",
"ListBoxDisabledForegroundThemeBrush",
"ListBoxFocusBackgroundThemeBrush",
"ListBoxForegroundThemeBrush",
"ListBoxItemDisabledForegroundThemeBrush",
"ListBoxItemPointerOverBackgroundThemeBrush",
"ListBoxItemPointerOverForegroundThemeBrush",
"ListBoxItemPressedBackgroundThemeBrush",
"ListBoxItemPressedForegroundThemeBrush",
"ListBoxItemSelectedBackgroundThemeBrush",
"ListBoxItemSelectedDisabledBackgroundThemeBrush",
"ListBoxItemSelectedDisabledForegroundThemeBrush",
"ListBoxItemSelectedForegroundThemeBrush",
"ListBoxItemSelectedPointerOverBackgroundThemeBrush",
"ListPickerFlyoutPresenterSelectedItemForegroundThemeBrush",
"ListPickerFlyoutPresenterSelectedItemBackgroundThemeBrush",
"ListViewGroupHeaderForegroundThemeBrush",
"ListViewGroupHeaderPointerOverForegroundThemeBrush",
"ListViewGroupHeaderPressedForegroundThemeBrush",
"ListViewItemCheckHintThemeBrush",
"ListViewItemCheckSelectingThemeBrush",
"ListViewItemCheckThemeBrush",
"ListViewItemDragBackgroundThemeBrush",
"ListViewItemDragForegroundThemeBrush",
"ListViewItemFocusBorderThemeBrush",
"ListViewItemOverlayBackgroundThemeBrush",
"ListViewItemOverlayForegroundThemeBrush",
"ListViewItemOverlaySecondaryForegroundThemeBrush",
"ListViewItemPlaceholderBackgroundThemeBrush",
"ListViewItemPointerOverBackgroundThemeBrush",
"ListViewItemSelectedBackgroundThemeBrush",
"ListViewItemSelectedForegroundThemeBrush",
"ListViewItemSelectedPointerOverBackgroundThemeBrush",
"ListViewItemSelectedPointerOverBorderThemeBrush",
"LoopingSelectorForegroundThemeBrush",
"LoopingSelectorSelectionBackgroundThemeBrush",
"LoopingSelectorSelectionForegroundThemeBrush",
"MediaButtonForegroundThemeBrush",
"MediaButtonBackgroundThemeBrush",
"MediaButtonPointerOverForegroundThemeBrush",
"MediaButtonPointerOverBackgroundThemeBrush",
"MediaButtonPressedForegroundThemeBrush",
"MediaButtonPressedBackgroundThemeBrush",
"MediaButtonPressedBorderThemeBrush",
"MediaControlPanelVideoThemeBrush",
"MediaControlPanelAudioThemeBrush",
"MediaDownloadProgressIndicatorThemeBrush",
"MediaErrorBackgroundThemeBrush",
"MediaTextThemeBrush",
"MenuFlyoutItemFocusedBackgroundThemeBrush",
"MenuFlyoutItemFocusedForegroundThemeBrush",
"MenuFlyoutItemDisabledForegroundThemeBrush",
"MenuFlyoutItemPointerOverBackgroundThemeBrush",
"MenuFlyoutItemPointerOverForegroundThemeBrush",
"MenuFlyoutItemPressedBackgroundThemeBrush",
"MenuFlyoutItemPressedForegroundThemeBrush",
"PivotForegroundThemeBrush",
"PivotHeaderBackgroundSelectedBrush",
"PivotHeaderBackgroundUnselectedBrush",
"PivotHeaderForegroundSelectedBrush",
"PivotHeaderForegroundUnselectedBrush",
"PivotNavButtonBackgroundThemeBrush",
"PivotNavButtonBorderThemeBrush",
"PivotNavButtonForegroundThemeBrush",
"PivotNavButtonPointerOverBackgroundThemeBrush",
"PivotNavButtonPointerOverBorderThemeBrush",
"PivotNavButtonPointerOverForegroundThemeBrush",
"PivotNavButtonPressedBackgroundThemeBrush",
"PivotNavButtonPressedBorderThemeBrush",
"PivotNavButtonPressedForegroundThemeBrush",
"MenuFlyoutSeparatorThemeBrush",
"ProgressBarBackgroundThemeBrush",
"ProgressBarBorderThemeBrush",
"ProgressBarForegroundThemeBrush",
"ProgressBarIndeterminateForegroundThemeBrush",
"RadioButtonBackgroundThemeBrush",
"RadioButtonBorderThemeBrush",
"RadioButtonContentDisabledForegroundThemeBrush",
"RadioButtonContentForegroundThemeBrush",
"RadioButtonDisabledBackgroundThemeBrush",
"RadioButtonDisabledBorderThemeBrush",
"RadioButtonDisabledForegroundThemeBrush",
"RadioButtonForegroundThemeBrush",
"RadioButtonPointerOverBackgroundThemeBrush",
"RadioButtonPointerOverBorderThemeBrush",
"RadioButtonPointerOverForegroundThemeBrush",
"RadioButtonPressedBackgroundThemeBrush",
"RadioButtonPressedBorderThemeBrush",
"RadioButtonPressedForegroundThemeBrush",
"RepeatButtonBorderThemeBrush",
"RepeatButtonDisabledBackgroundThemeBrush",
"RepeatButtonDisabledBorderThemeBrush",
"RepeatButtonDisabledForegroundThemeBrush",
"RepeatButtonForegroundThemeBrush",
"RepeatButtonPointerOverBackgroundThemeBrush",
"RepeatButtonPointerOverForegroundThemeBrush",
"RepeatButtonPressedBackgroundThemeBrush",
"RepeatButtonPressedForegroundThemeBrush",
"ScrollBarButtonForegroundThemeBrush",
"ScrollBarButtonPointerOverBackgroundThemeBrush",
"ScrollBarButtonPointerOverBorderThemeBrush",
"ScrollBarButtonPointerOverForegroundThemeBrush",
"ScrollBarButtonPressedBackgroundThemeBrush",
"ScrollBarButtonPressedBorderThemeBrush",
"ScrollBarButtonPressedForegroundThemeBrush",
"ScrollBarPanningBackgroundThemeBrush",
"ScrollBarPanningBorderThemeBrush",
"ScrollBarThumbBackgroundThemeBrush",
"ScrollBarThumbBorderThemeBrush",
"ScrollBarThumbPointerOverBackgroundThemeBrush",
"ScrollBarThumbPointerOverBorderThemeBrush",
"ScrollBarThumbPressedBackgroundThemeBrush",
"ScrollBarThumbPressedBorderThemeBrush",
"ScrollBarTrackBackgroundThemeBrush",
"ScrollBarTrackBorderThemeBrush",
"SearchBoxBackgroundThemeBrush",
"SearchBoxBorderThemeBrush",
"SearchBoxDisabledBackgroundThemeBrush",
"SearchBoxDisabledTextThemeBrush",
"SearchBoxDisabledBorderThemeBrush",
"SearchBoxPointerOverBackgroundThemeBrush",
"SearchBoxPointerOverTextThemeBrush",
"SearchBoxPointerOverBorderThemeBrush",
"SearchBoxFocusedBackgroundThemeBrush",
"SearchBoxFocusedTextThemeBrush",
"SearchBoxFocusedBorderThemeBrush",
"SearchBoxButtonBackgroundThemeBrush",
"SearchBoxButtonForegroundThemeBrush",
"SearchBoxButtonPointerOverForegroundThemeBrush",
"SearchBoxButtonPointerOverBackgroundThemeBrush",
"SearchBoxSeparatorSuggestionForegroundThemeBrush",
"SearchBoxHitHighlightForegroundThemeBrush",
"SearchBoxHitHighlightSelectedForegroundThemeBrush",
"SearchBoxIMECandidateListSeparatorThemeBrush",
"SearchBoxForegroundThemeBrush",
"SemanticZoomButtonBackgroundThemeBrush",
"SemanticZoomButtonBorderThemeBrush",
"SemanticZoomButtonForegroundThemeBrush",
"SemanticZoomButtonPointerOverBackgroundThemeBrush",
"SemanticZoomButtonPointerOverBorderThemeBrush",
"SemanticZoomButtonPointerOverForegroundThemeBrush",
"SemanticZoomButtonPressedBackgroundThemeBrush",
"SemanticZoomButtonPressedBorderThemeBrush",
"SemanticZoomButtonPressedForegroundThemeBrush",
"SettingsFlyoutBackgroundThemeBrush",
"SettingsFlyoutBackButtonPointerOverBackgroundThemeBrush",
"SettingsFlyoutHeaderBackgroundThemeBrush",
"SettingsFlyoutHeaderForegroundThemeBrush",
"SliderBorderThemeBrush",
"SliderDisabledBorderThemeBrush",
"SliderThumbBackgroundThemeBrush",
"SliderThumbBorderThemeBrush",
"SliderThumbDisabledBackgroundThemeBrush",
"SliderThumbPointerOverBackgroundThemeBrush",
"SliderThumbPointerOverBorderThemeBrush",
"SliderThumbPressedBackgroundThemeBrush",
"SliderThumbPressedBorderThemeBrush",
"SliderTickMarkInlineBackgroundThemeBrush",
"SliderTickMarkInlineDisabledForegroundThemeBrush",
"SliderTickmarkOutsideBackgroundThemeBrush",
"SliderTickMarkOutsideDisabledForegroundThemeBrush",
"SliderTrackBackgroundThemeBrush",
"SliderTrackDecreaseBackgroundThemeBrush",
"SliderTrackDecreaseDisabledBackgroundThemeBrush",
"SliderTrackDecreasePointerOverBackgroundThemeBrush",
"SliderTrackDecreasePressedBackgroundThemeBrush",
"SliderTrackDisabledBackgroundThemeBrush",
"SliderTrackPointerOverBackgroundThemeBrush",
"SliderTrackPressedBackgroundThemeBrush",
"SliderHeaderForegroundThemeBrush",
"TextBoxForegroundHeaderThemeBrush",
"TextBoxPlaceholderTextThemeBrush",
"TextBoxBackgroundThemeBrush",
"TextSelectionHighlightColorThemeBrush",
"TextBoxBorderThemeBrush",
"TextBoxButtonBackgroundThemeBrush",
"TextBoxButtonBorderThemeBrush",
"TextBoxButtonForegroundThemeBrush",
"TextBoxButtonPointerOverBackgroundThemeBrush",
"TextBoxButtonPointerOverBorderThemeBrush",
"TextBoxButtonPointerOverForegroundThemeBrush",
"TextBoxButtonPressedBackgroundThemeBrush",
"TextBoxButtonPressedBorderThemeBrush",
"TextBoxButtonPressedForegroundThemeBrush",
"TextBoxDisabledBackgroundThemeBrush",
"TextBoxDisabledBorderThemeBrush",
"TextBoxDisabledForegroundThemeBrush",
"TextBoxForegroundThemeBrush",
"ThumbBackgroundThemeBrush",
"ThumbBorderThemeBrush",
"ThumbPointerOverBackgroundThemeBrush",
"ThumbPointerOverBorderThemeBrush",
"ThumbPressedBackgroundThemeBrush",
"ThumbPressedBorderThemeBrush",
"TimePickerHeaderForegroundThemeBrush",
"TimePickerForegroundThemeBrush",
"ToggleButtonBackgroundThemeBrush",
"ToggleButtonBorderThemeBrush",
"ToggleButtonCheckedBackgroundThemeBrush",
"ToggleButtonCheckedBorderThemeBrush",
"ToggleButtonCheckedDisabledBackgroundThemeBrush",
"ToggleButtonCheckedDisabledForegroundThemeBrush",
"ToggleButtonCheckedForegroundThemeBrush",
"ToggleButtonCheckedPointerOverBackgroundThemeBrush",
"ToggleButtonCheckedPointerOverBorderThemeBrush",
"ToggleButtonCheckedPressedBackgroundThemeBrush",
"ToggleButtonCheckedPressedBorderThemeBrush",
"ToggleButtonCheckedPressedForegroundThemeBrush",
"ToggleButtonDisabledBorderThemeBrush",
"ToggleButtonDisabledForegroundThemeBrush",
"ToggleButtonForegroundThemeBrush",
"ToggleButtonPointerOverBackgroundThemeBrush",
"ToggleButtonPressedBackgroundThemeBrush",
"ToggleButtonPressedForegroundThemeBrush",
"ToggleSwitchCurtainBackgroundThemeBrush",
"ToggleSwitchCurtainDisabledBackgroundThemeBrush",
"ToggleSwitchCurtainPointerOverBackgroundThemeBrush",
"ToggleSwitchCurtainPressedBackgroundThemeBrush",
"ToggleSwitchDisabledForegroundThemeBrush",
"ToggleSwitchForegroundThemeBrush",
"ToggleSwitchHeaderDisabledForegroundThemeBrush",
"ToggleSwitchHeaderForegroundThemeBrush",
"ToggleSwitchOuterBorderBorderThemeBrush",
"ToggleSwitchOuterBorderDisabledBorderThemeBrush",
"ToggleSwitchThumbBackgroundThemeBrush",
"ToggleSwitchThumbBorderThemeBrush",
"ToggleSwitchThumbDisabledBackgroundThemeBrush",
"ToggleSwitchThumbDisabledBorderThemeBrush",
"ToggleSwitchThumbPointerOverBackgroundThemeBrush",
"ToggleSwitchThumbPointerOverBorderThemeBrush",
"ToggleSwitchThumbPressedBackgroundThemeBrush",
"ToggleSwitchThumbPressedForegroundThemeBrush",
"ToggleSwitchTrackBackgroundThemeBrush",
"ToggleSwitchTrackBorderThemeBrush",
"ToggleSwitchTrackDisabledBackgroundThemeBrush",
"ToggleSwitchTrackPointerOverBackgroundThemeBrush",
"ToggleSwitchTrackPressedBackgroundThemeBrush",
"ToolTipBackgroundThemeBrush",
"ToolTipBorderThemeBrush",
"ToolTipForegroundThemeBrush",
};
public string[] SystemBrushes = new string[] {
"SystemControlBackgroundAccentBrush",
"SystemControlBackgroundAltHighBrush",
"SystemControlBackgroundAltMediumHighBrush",
"SystemControlBackgroundAltMediumLowBrush",
"SystemControlBackgroundBaseHighBrush",
"SystemControlBackgroundBaseLowBrush",
"SystemControlBackgroundBaseMediumBrush",
"SystemControlBackgroundBaseMediumHighBrush",
"SystemControlBackgroundBaseMediumLowBrush",
"SystemControlBackgroundChromeBlackHighBrush",
"SystemControlBackgroundChromeBlackMediumBrush",
"SystemControlBackgroundChromeMediumBrush",
"SystemControlBackgroundChromeMediumLowBrush",
"SystemControlBackgroundChromeWhiteBrush",
"SystemControlBackgroundListLowBrush",
"SystemControlBackgroundListMediumBrush",
"SystemControlDisabledAccentBrush",
"SystemControlDisabledBaseHighBrush",
"SystemControlDisabledBaseLowBrush",
"SystemControlDisabledBaseMediumLowBrush",
"SystemControlDisabledChromeDisabledHighBrush",
"SystemControlDisabledChromeDisabledLowBrush",
"SystemControlDisabledChromeHighBrush",
"SystemControlDisabledChromeMediumLowBrush",
"SystemControlDisabledListMediumBrush",
"SystemControlDisabledTransparentBrush",
"SystemControlForegroundAccentBrush",
"SystemControlForegroundAltHighBrush",
"SystemControlForegroundAltMediumHighBrush",
"SystemControlForegroundBaseHighBrush",
"SystemControlForegroundBaseLowBrush",
"SystemControlForegroundBaseMediumBrush",
"SystemControlForegroundBaseMediumHighBrush",
"SystemControlForegroundBaseMediumLowBrush",
"SystemControlForegroundChromeBlackHighBrush",
"SystemControlForegroundChromeHighBrush",
"SystemControlForegroundChromeMediumBrush",
"SystemControlForegroundChromeWhiteBrush",
"SystemControlForegroundChromeDisabledLowBrush",
"SystemControlForegroundListLowBrush",
"SystemControlForegroundListMediumBrush",
"SystemControlForegroundTransparentBrush",
"SystemControlForegroundChromeBlackMediumBrush",
"SystemControlForegroundChromeBlackMediumLowBrush",
"SystemControlHighlightAccentBrush",
"SystemControlHighlightAltAccentBrush",
"SystemControlHighlightAltAltHighBrush",
"SystemControlHighlightAltBaseHighBrush",
"SystemControlHighlightAltBaseLowBrush",
"SystemControlHighlightAltBaseMediumBrush",
"SystemControlHighlightAltBaseMediumHighBrush",
"SystemControlHighlightAltAltMediumHighBrush",
"SystemControlHighlightAltBaseMediumLowBrush",
"SystemControlHighlightAltListAccentHighBrush",
"SystemControlHighlightAltListAccentLowBrush",
"SystemControlHighlightAltListAccentMediumBrush",
"SystemControlHighlightAltChromeWhiteBrush",
"SystemControlHighlightAltTransparentBrush",
"SystemControlHighlightBaseHighBrush",
"SystemControlHighlightBaseLowBrush",
"SystemControlHighlightBaseMediumBrush",
"SystemControlHighlightBaseMediumHighBrush",
"SystemControlHighlightBaseMediumLowBrush",
"SystemControlHighlightChromeAltLowBrush",
"SystemControlHighlightChromeHighBrush",
"SystemControlHighlightListAccentHighBrush",
"SystemControlHighlightListAccentLowBrush",
"SystemControlHighlightListAccentMediumBrush",
"SystemControlHighlightListMediumBrush",
"SystemControlHighlightListLowBrush",
"SystemControlHighlightTransparentBrush",
"SystemControlHyperlinkTextBrush",
"SystemControlHyperlinkBaseHighBrush",
"SystemControlHyperlinkBaseMediumBrush",
"SystemControlHyperlinkBaseMediumHighBrush",
"SystemControlPageBackgroundAltMediumBrush",
"SystemControlPageBackgroundMediumAltMediumBrush",
"SystemControlPageBackgroundBaseLowBrush",
"SystemControlPageBackgroundBaseMediumBrush",
"SystemControlPageBackgroundListLowBrush",
"SystemControlPageBackgroundChromeLowBrush",
"SystemControlPageTextBaseHighBrush",
"SystemControlPageTextBaseMediumBrush",
"SystemControlPageTextChromeBlackMediumLowBrush",
};
private void CheckBox_Checked(object sender, RoutedEventArgs e)
{
CheckBox cb = sender as CheckBox;
Frame rootFrame = Window.Current.Content as Frame;
rootFrame.RequestedTheme = cb.IsChecked.Value ? ElementTheme.Dark : ElementTheme.Light;
}
<ListView Name="_lvColors" SelectionMode="None" Margin="0 30 0 0">
<ListView.ItemTemplate>
<DataTemplate>
<Grid Height="48">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<TextBlock Text="{Binding ColorName}" VerticalAlignment="Center" IsTextSelectionEnabled="True" Margin="10 0"/>
<Grid Background="{Binding ColorBrush}" Grid.Column="1"/>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
<Setter Property="Padding" Value="0" />
<Setter Property="Margin" Value="0" />
</Style>
</ListView.ItemContainerStyle>
</ListView>
<CheckBox Content="Theme" Checked="CheckBox_Checked" Unchecked="CheckBox_Checked" HorizontalAlignment="Center" VerticalAlignment="Top"/>

show/hide control textbox based on combobox selected WPF

I have a WPF/MVVM project in C #/FrameWork 4.0
In my view I have two ControlBox "NoRSAC" and "LieuRSAC"
<View:StateControlTextBox
x:Name="NoRSAC"
ReadOnly="{Binding IsReadOnly}"
ViewModelDataType="UtilisateurSaisieViewModel"
TableDataType="TUtilisateurDataTable"
Tag="{DynamicResource TELEPHONE}"
Text="{Binding UserVM.No_RSAC, Mode=TwoWay}" Margin="0" Canvas.Top="140" Width="185" VerticalAlignment="Stretch" />
<View:StateControlTextBox
x:Name="LieuRSAC"
ReadOnly="{Binding IsReadOnly}"
ViewModelDataType="UtilisateurSaisieViewModel"
TableDataType="TUtilisateurDataTable"
Tag="{DynamicResource TELEPHONE}"
Text="{Binding UserVM.Lieu_RSAC, Mode=TwoWay}" Margin="0" Canvas.Top="140" Width="185" VerticalAlignment="Stretch"/>
</Canvas>
And ControlComboBox "cmbFonction"
<View:StateControlComboBox
x:Name="cmbFonction"
ReadOnlyControlState="Disabled"
IsReadOnly="{Binding IsReadOnly}"
ViewModelDataType="UtilisateurSaisieViewModel"
TableDataType="TUtilisateurDataTable"
ItemsSource="{Binding ListeFonctions}"
SelectedValue="{Binding UserVM.Fonction, Mode=TwoWay}" Width="303" Margin="0" HorizontalAlignment="Left" Canvas.Left="97" Canvas.Top="108" />
I want to view the ControlBox "NoRSAC" and "LieuRSAC" when I select a particular valeure in the ComboBox "cmbFonction" and hide when it's another selected value
Thank you for your help
In the set method of the property Fonction, you can check the value and update another property that you should introduce in your view model and that is of type System.Windows.Visibility. In the following example, I call this property TextBoxVisibility:
public class UserVM : INotifyPropertyChanged
{
private Visibility _textBoxVisibility;
public Visibility TextBoxVisibility
{
get { return _textBoxVisibility; }
set
{
_textBoxVisibility = value;
OnPropertyChanged();
}
}
public string Fonction
{
get { return _fonction; }
set
{
_fonction = value;
OnPropertyChanged();
if (value == "Value A")
TextBoxVisibility = Visibility.Hidden;
else
TextBoxVisibility = Visibility.Visible;
}
}
// Other members omitted for sake of simplicity.
}
Please note that you need to implement INotifyPropertyChanged (directly or indirectly) so that the changes of the property values are forwarded to the bindings that can in turn update the dependency properties of the controls in your view.
Thus you must not forget to add an additional binding to all of your text boxes in your view. Here is an example for that, the important part is the binding on Visibility:
<View:StateControlTextBox
x:Name="NoRSAC"
ReadOnly="{Binding IsReadOnly}"
ViewModelDataType="UtilisateurSaisieViewModel"
TableDataType="TUtilisateurDataTable"
Tag="{DynamicResource TELEPHONE}"
Visibility="{Binding UserVM.TextBoxVisibility}"
Text="{Binding UserVM.No_RSAC, Mode=TwoWay}" Margin="0" Canvas.Top="140" Width="185" VerticalAlignment="Stretch" />

ZoomExtents method call works different than activating ZoomExtents through gesture

I've been working on a small 3D preview window in a MVVM style application... The view is created then its data context is set. Therefore it seems that ZoomExtentsWhenLoaded="True" doesn't seem to help do what I need. I need something like, ZoomExtentsWhenDataContextChanges.
Interestingly, I've found that if I use a mouse gesture like the one defined below, I can physically click on the HelixViewport3D and it will perform a ZoomExtents.
HelixViewport3D.ZoomExtentsGesture = new MouseGesture(MouseAction.LeftDoubleClick);
However, if do something like this...
HelixViewport3D.DataContextChanged += (o, e) => ResetCamera();
private void ResetCamera()
{
var dc = HelixViewport3D.DataContext as WellSurveyPlot3DViewModel;
HelixViewport3D.ResetCamera();
HelixViewport3D.Camera = dc.PerspectiveCamera;
HelixViewport3D.ZoomExtents();
}
The viewport does zoom, it just doesn't center itself, like it does when activating ZoomExtents when using the mouse gesture.
I tried ResetCamera, and several other things... What is the standard way of dealing with keeping a viewport around and swapping out the DataContext instead of creating a new one each time?
I fixed this with an attached property. I read through the HelixViewport3D source code and got this idea, after noticing how the camera works. It seems an update to the default camera through a property binding doesn't really do anything after the control is initialized.
public static class HelixViewport3DZoomExtent
{
private static readonly Type OwnerType = typeof(HelixViewport3DZoomExtent);
public static readonly DependencyProperty ZoomExtentsOnUpdateProperty = DependencyProperty.RegisterAttached("ZoomExtentsOnUpdate", typeof(bool), OwnerType, new PropertyMetadata(false, OnDataContextChanged));
public static bool GetZoomExtentsOnUpdate(DependencyObject obj)
{
return (bool)obj.GetValue(ZoomExtentsOnUpdateProperty);
}
public static void SetZoomExtentsOnUpdate(DependencyObject obj, bool value)
{
obj.SetValue(ZoomExtentsOnUpdateProperty, value);
}
private static void OnDataContextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var viewport = d as HelixViewport3D;
if (viewport == null) return;
if (viewport.DataContext == null) return;
viewport.Camera = viewport.DefaultCamera;
viewport.ZoomExtents();
}
}
Here is the Xaml
<Border BorderBrush="Black" BorderThickness="1">
<Grid>
<h:HelixViewport3D Name="HelixViewport3D"
PanGesture="LeftClick"
DataContext="{Binding PreviewPlot, UpdateSourceTrigger=PropertyChanged}"
DefaultCamera="{Binding PerspectiveCamera, UpdateSourceTrigger=PropertyChanged}"
services:HelixViewport3DZoomExtent.ZoomExtentsOnUpdate="{Binding RelativeSource={RelativeSource AncestorType={x:Type views:WellSurveyPlot3DPreview}},
Path=DataContext.PreviewUpdatedReZoom, UpdateSourceTrigger=PropertyChanged}">
<h:SunLight/>
<h:TubeVisual3D Path="{Binding TubePath}" Diameter="75" ThetaDiv="12" IsPathClosed="False" Fill="LightGray"/>
<h:GridLinesVisual3D Width="{Binding GridLength}" Length="{Binding GridLength}" MajorDistance="{Binding MajorGridLines}" Thickness="25"
MinorDistance="{Binding MajorGridLines, UpdateSourceTrigger=PropertyChanged}" LengthDirection="1,0,0" Normal="0,0,1"
Center="{Binding BottomPlaneCenter,UpdateSourceTrigger=PropertyChanged}" Fill="Red" />
<h:GridLinesVisual3D Width="{Binding GridLength}" Length="{Binding GridLength, UpdateSourceTrigger=PropertyChanged}" LengthDirection="0,0,1" Normal="1,0,0" Thickness="25"
MajorDistance="{Binding MajorGridLines}" MinorDistance="{Binding MajorGridLines}"
Center="{Binding BackLeftPlaneCenter, UpdateSourceTrigger=PropertyChanged}" Fill="Blue" />
<h:GridLinesVisual3D Width="{Binding GridLength}" Length="{Binding GridLength, UpdateSourceTrigger=PropertyChanged}" LengthDirection="1,0,0" Normal="0,1,0" Thickness="25"
MajorDistance="{Binding MajorGridLines}" MinorDistance="{Binding MajorGridLines}"
Center="{Binding BackRightPlaneCenter,UpdateSourceTrigger=PropertyChanged}" Fill="Green" />
</h:HelixViewport3D>
<Button Content="Open Well Viewer" HorizontalAlignment="Left" VerticalAlignment="Top" Command="{Binding OpenWindowCmd}"/>
</Grid>
</Border>
In my view model I have to toggle my PreviewUpdateReZoom property.
private void LoadSurveyPoints(List<WellSurveyPointCalculated> surveyPoints)
{
_coordinatesCalculator = _calcGlobalCoordsFactory.Create(surveyPoints);
_wellXyzCoordinates = _coordinatesCalculator.PlotGlobalCoordinates(100).ToList();
PreviewPlot = WellSurveyPlot3DViewModel();
PreviewUpdatedReZoom = false;//Toggle true false to send property changed and get attached property to fire.
PreviewUpdatedReZoom = true;
}
This now works such that every new item drawn into the viewport has the correct camera settings and zooms to extents...

Categories