With a checkbox I want to enable or disable a datepicker, but my code doesn't work.
This is the xaml
<CheckBox HorizontalAlignment="Left" Height="32"
IsChecked="{Binding ABC}"
Margin="108,91,0,0" VerticalAlignment="Top" Width="191"/>
<DatePicker HorizontalAlignment="Left" Height="32"
IsEnabled="{Binding ABC}"
Margin="142,91,0,0" VerticalAlignment="Top" Width="217"/>
And this is the property.
private bool _ABC {
get;
set;
}
public bool ABC {
get {
return _ABC;
}
set {
_ABC = value;
}
}
private void Window_Loaded(object sender, RoutedEventArgs e) {
MainWindow Vm = new MainWindow();
this.DataContext = Vm;
ABC = false;
}
Thank you
You need to tell it what property it's looking for, and who to get it from, like;
<CheckBox x:Name="TheCheckBox"
HorizontalAlignment="Left" Height="32"
IsChecked="{Binding ABC}"
Margin="108,91,0,0" VerticalAlignment="Top" Width="191"/>
<DatePicker HorizontalAlignment="Left" Height="32"
IsEnabled="{Binding IsChecked, ElementName=TheCheckBox}"
Margin="142,91,0,0" VerticalAlignment="Top" Width="217"/>
Hope this helps, cheers.
Related
The UserControl is composed of two labels and one Textbox.Some relative codes are below
public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
DataContext = this;
}
public string Title { get; set; }
public string Company {get; set; }
public TextBox TrueText;
private void Input_Loaded(object sender, RoutedEventArgs e)
{
this.TrueText = sender as TextBox;
}
}
xaml
<Viewbox >
<Grid HorizontalAlignment="Left" VerticalAlignment="Center">
<Label Content="{Binding Title}" HorizontalAlignment="Left" Height="20" VerticalAlignment="Center" Padding="0,0,0,0" VerticalContentAlignment="Center" HorizontalContentAlignment="Center" Width="180"/>
<TextBox Margin="180,0,0,0" Name="Input" Loaded="Input_Loaded" Controls:TextBoxHelper.Watermark="待输入" Controls:TextBoxHelper.ClearTextButton="True" Padding="0,0,0,0" VerticalContentAlignment="Center" MinHeight="0" HorizontalAlignment="Left" VerticalAlignment="Center" Width="120" Height="18"/>
<Label Content="{Binding Company}" HorizontalAlignment="Left" Height="18" Margin="300,1,0,0" VerticalAlignment="Top" Width="50" Padding="0,0,0,0" HorizontalContentAlignment="Center" VerticalContentAlignment="Center"/>
</Grid>
</Viewbox>
Then I want to get the Input in TextBox of each UserControl.But I can't get the children of the UniformGrid.
I have tried to use GetChildObject, but in this way I can't access his related properties
This is my wrong codeyour text
IEnumerable<UserControl1> list = (IEnumerable<UserControl1>)Grid1.GetChildObjects();
int i = 1;
double[] num = { 0 };
foreach (var u in list)
{
i++;
num[i] = Convert.ToDouble(u.TrueText.Text);
}
Any solution will be truly appreciated
I have two radio buttons in a group as part of my XAML project:
<StackPanel HorizontalAlignment="Left" VerticalAlignment="Center" Orientation="Horizontal" Grid.Column="0" Margin="20,0,0,0">
<RadioButton x:Name="XMLViewButton" GroupName="DisplayType" IsChecked="{Binding XmlViewIsChecked, FallbackValue=True, Mode=TwoWay}" Content="XML View" Margin="0,0,5,0"/>
<RadioButton x:Name="TextViewButton" GroupName="DisplayType" IsChecked="{Binding TextViewIsChecked, FallbackValue=False, Mode=TwoWay}" Content="Text View" Margin="5,0,0,0"/>
</StackPanel>
And I then have a command later on which refers to these IsChecked bindings:
public void CopyToClipboard(object o)
{
if (TextViewIsSelected == true)
{
Clipboard.SetText(myFile.TextContent);
}
else if (XmlViewIsSelected == true)
{
Clipboard.SetText(myFile.XMLContent);
}
}
However, the XmlViewIsSelected is permanently True and TextViewIsSelected is always false, no matter which radio button is selected. What am I missing?
I think you are mispelling XmlViewIsChecked with XmlViewIsSelected
The following for me is working
<StackPanel HorizontalAlignment="Left" VerticalAlignment="Center" Orientation="Horizontal" Grid.Column="0" Margin="20,0,0,0">
<RadioButton x:Name="XMLViewButton" GroupName="DisplayType" IsChecked="{Binding XmlViewIsChecked, FallbackValue=True, Mode=TwoWay}" Content="XML View" Margin="0,0,5,0"/>
<RadioButton x:Name="TextViewButton" GroupName="DisplayType" IsChecked="{Binding TextViewIsChecked, FallbackValue=False, Mode=TwoWay}" Content="Text View" Margin="5,0,0,0"/>
<Button Content="Check" Click="Button_Click" />
</StackPanel>
public partial class MainWindow : Window
{
public bool XmlViewIsChecked { get; set; }
public bool TextViewIsChecked { get; set; }
public MainWindow()
{
InitializeComponent();
DataContext = this;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
if (TextViewIsChecked)
{
Clipboard.SetText("text");
}
else if (XmlViewIsChecked)
{
Clipboard.SetText("xml");
}
}
}
I have created a combo box and im binding in MVVM pattern, but my properties are not binding to the view, I'm confused with itemsource and selectvalue.
What changes can I make to view.xaml? I'm guessing the remainder of the code in my model and view model are perfect.
This is my Model
namespace screensaver.Models {
class ConfigurationModel {
public int _resolution;
private ObservableCollection < ConfigurationModel > Resolution {
get {
return Resolution;
}
set {
Resolution = value;
}
}
public ConfigurationModel() {
Resolution = new ObservableCollection < ConfigurationModel > () {
new ConfigurationModel() {
_resolution = 360 * 720
},
new ConfigurationModel() {
_resolution = 720 * 1080
},
new ConfigurationModel() {
_resolution = 1080 * 2060
}
};
}
}
}
This is my ViewModel
namespace screensaver.ViewModels {
class ConfigurationViewModel {
private ObservableCollection < ConfigurationModel > _resolution;
public ObservableCollection < ConfigurationModel > Resolution {
get {
return Resolution;
}
set {
Resolution = value;
}
}
}
}
This is my View xaml code
<Window x:Class="screensaver.Views.ConfigurationWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:ViewModels="clr-namespace:screensaver.ViewModels" Title="ConfigurationWindow"
Height="1000" Width="500">
<Grid>
<Label Content="Display" HorizontalAlignment="Left" Margin="7,12,0,0" VerticalAlignment="Top" />
<ComboBox HorizontalAlignment="Left" Margin="322,14,0,0" VerticalAlignment="Top" Width="120" />
<ComboBox ItemsSource="{Binding Resolution}" SelectedItem="{Binding
Resolution, Mode=TwoWay}" DisplayMemberPath="{Binding Resolution}" HorizontalAlignment="Left" Margin="74,13,0,0" VerticalAlignment="Top" Width="120" />
<Label Content="Resolution" HorizontalAlignment="Left" Margin="250,13,0,0" VerticalAlignment="Top" />
<Button Content="Save" HorizontalAlignment="Left" Margin="80,362,0,0" VerticalAlignment="Top" Width="75" />
<Button Content="Close" HorizontalAlignment="Left" Margin="350,360,0,0" VerticalAlignment="Top" Width="75" />
<Label Content="Height" HorizontalAlignment="Left" Margin="72,178,0,0" VerticalAlignment="Top" />
<TextBox HorizontalAlignment="Left" Height="23" Margin="140,181,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120" />
<Label Content="Width" HorizontalAlignment="Left" Margin="290,175,0,0" VerticalAlignment="Top" />
<TextBox HorizontalAlignment="Left" Height="23" Margin="346,179,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120" />
<Label Content="Top" HorizontalAlignment="Left" Margin="76,253,0,0" VerticalAlignment="Top" />
<TextBox HorizontalAlignment="Left" Height="23" Margin="140,255,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120" />
<Label Content="Left" HorizontalAlignment="Left" Margin="292,250,0,0" VerticalAlignment="Top" />
<TextBox HorizontalAlignment="Left" Height="23" Margin="349,252,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120" />
</Grid>
</Window>
There are some problems with your code.
First fix your property Resolution in ViewModel to prevent a StackOverflowException. Use your field _resolution in get and set.
private ObservableCollection < ConfigurationModel > Resolution {
get {
return _resolution;
}
set {
_resolution = value;
}
}
Similar problem in your Model. Here you can use an auto-property
private ObservableCollection<ConfigurationModel> Resolution
{
get;
set;
}
Maybe you should also exchange the ObservableCollection by a List<>. But this is not necessary. The field _resolution can be removed and the type of Resolution poperty changed to ObservableCollection< string >.
private ObservableCollection<string> Resolution
{
get;
set;
}
Your constructor can then be changed to
public ConfigurationModel()
{
Resolution = new ObservableCollection<string>() {
"360 * 720",
"720 * 1080",
"1080 * 2060"
};
}
There is also missing the link from Model to ViewModel. Something like that:
private readonly ConfigurationModel _model;
public ConfigurationViewModel()
{
_model = new ConfigurationModel();
}
And then you have to use it, so you have to change your property
public ObservableCollection<string> Resolution
{
get
{
return _model.Resolution;
}
set
{
_model.Resolution = value;
}
}
Therefore you have to change the modifier in the Model from private to public.
public ObservableCollection<string> Resolution
{
get;
set;
}
Now you can remove the field _resolution from ViewModel.
DisplayMemberPath have to be removed from the View. And you have to set the DataContext properly.
<Window.DataContext>
<ViewModels:ConfigurationViewModel />
</Window.DataContext>
So far you have that result:
The SelectedItem in the View have to bind to another property in the ViewModel.
public string SelectedResolution { get; set; }
SelectedItem="{Binding SelectedResolution, Mode=TwoWay}"
This should be a good starting point to go further on. You can change the string in the ObservableCollection to an own type with more properties. Then you need to set the DisplayMemberPath again.
Here is the final code.
Model:
using System.Collections.ObjectModel;
namespace screensaver.Models
{
class ConfigurationModel
{
public ObservableCollection<string> Resolution
{
get;
set;
}
public ConfigurationModel()
{
Resolution = new ObservableCollection<string>() {
"360 * 720",
"720 * 1080",
"1080 * 2060"
};
}
}
}
ViewModel:
using screensaver.Models;
using System.Collections.ObjectModel;
namespace screensaver.ViewModels
{
class ConfigurationViewModel
{
private readonly ConfigurationModel _model;
public ConfigurationViewModel()
{
_model = new ConfigurationModel();
}
public ObservableCollection<string> Resolution
{
get { return _model.Resolution; }
set { _model.Resolution = value; }
}
public string SelectedResolution { get; set; }
}
}
View:
<Window x:Class="screensaver.Views.ConfigurationWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:ViewModels="clr-namespace:screensaver.ViewModels" Title="ConfigurationWindow"
Height="1000" Width="500">
<Window.DataContext>
<ViewModels:ConfigurationViewModel />
</Window.DataContext>
<Grid>
<Label Content="Display" HorizontalAlignment="Left" Margin="7,12,0,0" VerticalAlignment="Top" />
<ComboBox HorizontalAlignment="Left" Margin="322,14,0,0" VerticalAlignment="Top" Width="120" />
<ComboBox ItemsSource="{Binding Resolution}" SelectedItem="{Binding SelectedResolution, Mode=TwoWay}" HorizontalAlignment="Left" Margin="74,13,0,0" VerticalAlignment="Top" Width="120" />
<Label Content="Resolution" HorizontalAlignment="Left" Margin="250,13,0,0" VerticalAlignment="Top" />
<Button Content="Save" HorizontalAlignment="Left" Margin="80,362,0,0" VerticalAlignment="Top" Width="75" />
<Button Content="Close" HorizontalAlignment="Left" Margin="350,360,0,0" VerticalAlignment="Top" Width="75" />
<Label Content="Height" HorizontalAlignment="Left" Margin="72,178,0,0" VerticalAlignment="Top" />
<TextBox HorizontalAlignment="Left" Height="23" Margin="140,181,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120" />
<Label Content="Width" HorizontalAlignment="Left" Margin="290,175,0,0" VerticalAlignment="Top" />
<TextBox HorizontalAlignment="Left" Height="23" Margin="346,179,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120" />
<Label Content="Top" HorizontalAlignment="Left" Margin="76,253,0,0" VerticalAlignment="Top" />
<TextBox HorizontalAlignment="Left" Height="23" Margin="140,255,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120" />
<Label Content="Left" HorizontalAlignment="Left" Margin="292,250,0,0" VerticalAlignment="Top" />
<TextBox HorizontalAlignment="Left" Height="23" Margin="349,252,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120" />
</Grid>
</Window>
I'm getting a strange behavior in a test model for mvvm-light:
I created one Window and setted two resources, one grid with two distinct dataContext, the problem is that the two view models are receiving the response even if I'm using only the side of one dataContext. Is there a way to set only the specified data context to fire to the view model that I indicate?
XAML Code:
<Window x:Class="POCWPFValidation.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vm="clr-namespace:POCWPFValidation.ViewModel"
Title="MainWindow" Height="350" Width="525" >
<Window.Resources>
<vm:MainViewModel x:Key="Main"/>
<vm:SecondaryView x:Key="Sec"/>
</Window.Resources>
<Grid>
<StackPanel Orientation="Horizontal">
<StackPanel Orientation="Vertical" DataContext="{Binding Source={StaticResource Main}}">
<CheckBox Content="CheckBox" IsChecked="{Binding CheckBox1Checked}" HorizontalAlignment="Left" VerticalAlignment="Top"/>
<CheckBox Content="CheckBox" IsChecked="{Binding CheckBox2Checked}" HorizontalAlignment="Left" VerticalAlignment="Top"/>
<CheckBox Content="CheckBox" IsChecked="{Binding CheckBox3Checked}" HorizontalAlignment="Left" VerticalAlignment="Top"/>
<CheckBox Content="CheckBox" IsChecked="{Binding CheckBox4Checked}" HorizontalAlignment="Left" VerticalAlignment="Top"/>
<TextBox Text="{Binding StringTest, UpdateSourceTrigger=PropertyChanged}" />
<Button Content="Button" Command="{Binding Teste}" HorizontalAlignment="Left" VerticalAlignment="Top" Background="{x:Null}" DataContext="{Binding Mode=OneWay}"/>
<Button Content="Button" Command="{Binding Teste2}" CommandParameter="{Binding ElementName=TextBox1, Path=Text}" HorizontalAlignment="Left" VerticalAlignment="Top" Background="{x:Null}" DataContext="{Binding Mode=OneWay}"/>
</StackPanel>
<StackPanel Orientation="Vertical" DataContext="{Binding Source={StaticResource Sec}}">
<CheckBox Content="CheckBox" IsChecked="{Binding CheckBox1Checked}" HorizontalAlignment="Left" VerticalAlignment="Top"/>
<CheckBox Content="CheckBox" IsChecked="{Binding CheckBox2Checked}" HorizontalAlignment="Left" VerticalAlignment="Top"/>
<CheckBox Content="CheckBox" IsChecked="{Binding CheckBox3Checked}" HorizontalAlignment="Left" VerticalAlignment="Top"/>
<CheckBox Content="CheckBox" IsChecked="{Binding CheckBox4Checked}" HorizontalAlignment="Left" VerticalAlignment="Top"/>
<TextBox Text="{Binding StringTest, UpdateSourceTrigger=PropertyChanged}" />
<Button Content="Button" Command="{Binding Teste}" HorizontalAlignment="Left" VerticalAlignment="Top" Background="{x:Null}" DataContext="{Binding Mode=OneWay}"/>
<Button Content="Button" Command="{Binding Teste2}" CommandParameter="{Binding ElementName=TextBox1, Path=Text}" HorizontalAlignment="Left" VerticalAlignment="Top" Background="{x:Null}" DataContext="{Binding Mode=OneWay}"/>
</StackPanel>
</StackPanel>
</Grid>
</Window>
The views are the same, so just change the name to SecondaryView
namespace POCWPFValidation.ViewModel
{
public class MainViewModel : ViewModelBase
{
private bool m_blnCheckBox1Checked;
private bool m_blnCheckBox2Checked;
private bool m_blnCheckBox3Checked;
private bool m_blnCheckBox4Checked;
public bool CheckBox1Checked
{
get { return m_blnCheckBox1Checked; }
set
{
Set(ref m_blnCheckBox1Checked, value, true);
}
}
public bool CheckBox2Checked
{
get { return m_blnCheckBox2Checked; }
set
{
Set(ref m_blnCheckBox2Checked, value, true);
}
}
public bool CheckBox3Checked
{
get { return m_blnCheckBox3Checked; }
set
{
Set(ref m_blnCheckBox3Checked, value, true);
}
}
public bool CheckBox4Checked
{
get { return m_blnCheckBox4Checked; }
set
{
Set(ref m_blnCheckBox4Checked, value, true);
}
}
string m_strStringTest = string.Empty;
public string StringTest
{
get { return m_strStringTest; }
set
{
Set(ref m_strStringTest, value, true);
}
}
public RelayCommand<string> Teste2 { get; private set; }
public RelayCommand Teste { get; private set; }
public MainViewModel()
{
Teste = new RelayCommand(MyAction, MyCanExecute);
Teste2 = new RelayCommand<string>(MyAction2, MyCanExecute2);
}
private void MyAction()
{
}
private void MyAction2(string seila)
{
}
private bool MyCanExecute2(string teste)
{
return !string.IsNullOrWhiteSpace(teste);
}
private bool MyCanExecute()
{
if (CheckBox1Checked && CheckBox2Checked && CheckBox3Checked && CheckBox4Checked && StringTest.Equals("teste"))
{
return true;
}
return false;
}
}
}
EDIT:
Sorry, I was not clear with my question, let me try again: like I told before, I have two view models and two distinct datacontext in mainWindow one for each viewmodel. Every time some property on any of these view models changes all the canExecute method of the two viewmodels are triggered. Change some property on view A and the can execute will be triggered on views A and B. Is there a way to only trigger at the specific CanExecute responsible view?
CanExecute is not triggered by the property changes but by CommandManager.RequerySuggested event. Don't worry about when its invoked WPF handles that
I have a window
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:System="clr-namespace:System;assembly=mscorlib" xmlns:GRHelper="clr-namespace:Enertek.GRHelper;assembly=Enertek.GRHelper" xmlns:local="clr-namespace:EneGR" x:Class="EneGR.SearchWindow"
Title="Main Window" Height="274.753" Width="322.345" Icon="icon.ico">
<Window.OpacityMask>
<RadialGradientBrush>
<GradientStop Color="Black" Offset="0"/>
<GradientStop Color="White" Offset="1"/>
</RadialGradientBrush>
</Window.OpacityMask>
<Window.BorderBrush>
<ImageBrush Stretch="None" TileMode="FlipXY"/>
</Window.BorderBrush>
<Grid RenderTransformOrigin="0.5,0.5" Margin="0,0,2,6">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="auto"/>
<ColumnDefinition Width="auto" MinWidth="120"/>
<ColumnDefinition Width="65"/>
<ColumnDefinition Width="120"/>
<ColumnDefinition Width="70"/>
</Grid.ColumnDefinitions>
<Button x:Name="SearchButton" Content="Search" Grid.Column="3" Margin="0,62,0,0" VerticalAlignment="Top" Height="23" RenderTransformOrigin="0.6,0.458" FontWeight="Bold"
Click="SearchButton_Click"/>
<Label x:Name="SearchLabel" Content="Enter object's name or its part in the field below:" HorizontalAlignment="Left" Margin="0,10,0,0" VerticalAlignment="Top" Grid.ColumnSpan="4" Width="285" Height="36"/>
<Button x:Name="SearchCancel" Grid.ColumnSpan="2" Content="Cancel" HorizontalAlignment="Left" Margin="0,61,0,0" VerticalAlignment="Top" Width="120" Height="24" FontWeight="Bold"
Click=" SearchCancel_Click"/>
<TreeView Grid.Column ="1" ItemsSource="{Binding objs}" Margin="0,101,3,40" Grid.ColumnSpan="3" Background="White">
<TreeView.DataContext>
<local:MyTreeData/>
</TreeView.DataContext>
<TreeView.ItemTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding Checked, Mode=TwoWay}" Content="{Binding TagName}" Checked="CheckBox_Checked"/>
</DataTemplate>
</TreeView.ItemTemplate>
</TreeView>
<Button x:Name="Rename" Content="Rename" Grid.Column="2" HorizontalAlignment="Left" Margin="62,212,0,0" VerticalAlignment="Top" Width="120" Grid.ColumnSpan="2" Click="Rename_Click" FontWeight="Bold"/>
<TextBox x:Name="TagNameBox" Grid.ColumnSpan="3" Grid.Column="1" HorizontalAlignment="Left" Height="23" Margin="10,33,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="285"/>
<Button Content="Button" HorizontalAlignment="Left" Height="100" Margin="-106,257,0,-115" VerticalAlignment="Top" Width="75"/>
</Grid>
</Window>
The code behind:
namespace EneGR
{
public class MyTreeData
{
public ObservableCollection<GRObject> objs { get; set; }
private bool allAreChecked = false;
public MyTreeData()
{
objs = new ObservableCollection<GRObject>();
}
public bool AllAreChecked
{
get
{
return allAreChecked;
}
set
{
allAreChecked = value;
}
}
}
/// <summary>
/// Interaction logic for SearchWindow.xaml
/// </summary>
public partial class SearchWindow : Window
{
GRGalaxy galaxy = EneGR.LoginWin.galaxy;
List <GRObject> ObjsToRename = null;
public MyTreeData TreeData = new MyTreeData();
public SearchWindow()
{
InitializeComponent();
DataContext = TreeData;
}
private void SearchCancel_Click(object sender, RoutedEventArgs e)
{
Close();
}
private void SearchButton_Click(object sender, RoutedEventArgs e)
{
string TagName = TagNameBox.Text;
IEnumerable<GRObject> objects = galaxy.QueryObjectsByName(TagName + '%');
MessageBox.Show(objects.Count().ToString() + " " + "Objects found" );
foreach (GRObject obj in objects)
{
TreeData.objs.Add(obj);
};
}
private void Rename_Click(object sender, RoutedEventArgs e)
{
galaxy.RenameCheckedObjects(ObjsToRename);
}
private void CheckBox_Checked(object sender, RoutedEventArgs e)
{
//add checked object to ienumerable collection ObjsToRename;
}
}
}
I binded objects from observable collection TreeData.objs to TreeView with CheckBoxes but they never show up in the window. I cannot understand what is wrong. IEnumerable objects is not null.
You can remove this code :
<TreeView.DataContext>
<local:MyTreeData/>
</TreeView.DataContext>
... which creates a new instance of MyTreeData and sets the TreeView's DataContext to it.
The context of your treeview is already set to the MyTreeData you want to use (because of DataContext inheritance) :
public SearchWindow()
{
InitializeComponent();
DataContext = TreeData;
}