I'm using VB.Net & WPF (not using MVVM as I'm not familiar with it)
I have a Toolbar control in my User Control Style whose Visibility needs to be set from code behind so I decided to use BooleanToVisibilityConverter. I have a Boolean variable in my code depending on whose value toolbar visibility needs to be set.
How can I achieve this.
Thanks in advance
Edit
Tried following
XAML
<Window.Resources>
<BooleanToVisibilityConverter x:Key="BooleanToVisibility"/>
</Window.Resources>
<ToolBarTray Visibility="{Binding Converter={StaticResource BooleanToVisibility}}"/>
VB.Net Code
Dim IsToolbarVisible As Boolean = True
Public Property ToolbarVisibality As Boolean
Get
Return IsToolbarVisible
End Get
Set(value As Boolean)
IsToolbarVisible = value
End Set
End Property
How to Pass value of IsToolBarVisisble to Boolean TO Visibility Converter ???
Right now your binding uses the DataContext as value. You'll want to set the DataContext to the object holding a public IsToolbarVisible property (is that a property? looks like a field, i don't speak vb) and set the Binding.Path to IsToolbarVisible. Read the data binding overview.
Related
I have a control that is inheriting another third party control to extend some base functionality. As a result of this, I can't also inherit DependencyObject.
I set up an attached DependencyProperty via the follow manner on my AerosWpfMap control:
Public Shared ReadOnly Property CountyCodesProperty As DependencyProperty = DependencyProperty.Register("CountyCodes", GetType(SystemCountyCodes), GetType(AerosWpfMap), New FrameworkPropertyMetaData(Nothing))
And the Property is defined as follows:
Public Property CountyCodes As SystemCountyCodes
Get
Return Dispatcher.Invoke(Function()
Return GetValue(CountyCodesProperty)
End Function)
End Get
Set
SetValue(CountyCodesProperty, value)
End Set
End Property
The tricky part here is that the SystemCountyCodes class inherits a List(Of CountyCodeInfo).
Now, I have a window class called UI that contains this AerosWpfMap control and I am trying to bind the CountyCodes property of the AerosWpfMap to the CountyCodes property in my UI class. I implemented the INotifyPropertyChanged in my window and raise the event during the setter of the CountyCodes property as indicated below:
Public Property CountyCodes As SystemCountyCodes
Get
Return _countyCodes
End Get
Set
_countyCodes = value
RaiseEvent INotifyPropertyChanged_PropertyChanged(Me, New PropertyChangedEventArgs("CountyCodes"))
End Set
End Property
And the binding in my Xaml of the UI.xaml is as follow:
<mapping:AerosWpfMap x:Name="Map"
Grid.Column="1"
HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
Background="{StaticResource CheckerBackground}"
CountyCodes="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type mapping:UI}}, Path=CountyCodes, Mode=OneWay}"
Srid="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type mapping:UI}}, Path=Srid, Mode=OneWay}" />
Ironically enough, it doesn't appear to work unless I specify the relative source that way, even though I should be able to use RelativeSource={RelativeSource Self}.
This all works and binds fine when it runs; however, I am still presented with the following error
Severity Code Description Project File Line Suppression State
Error A 'Binding' cannot be set on the 'CountyCodes' property of type 'Controls_Mapping_AerosWpfMap_17_362199327'. A 'Binding' can only be set on a DependencyProperty of a DependencyObject. AerosAdjusterMapping C:\Projects\AerosAdjusterMapping\AerosAdjusterMapping\Controls\Mapping\UI.xaml 383
This leads me to believe I still have something set up incorrectly. Can anyone point me in the right direction?
I think I may either have the concept of the DependencyProperty backwards (i.e. it should be on the target (UI) and not the Source(AerosWpfMap), or that I need to have my AerosWpfMap control inherit from a DependencyObject, but as I stated, since it already inherits from another control I cannot go this route.
Any help would be greatly appreciated, and apologies for it being in VB (it is one of the requirements for this). I am fluent in C# as well so any help in either language would be appreciated.
Thanks.
I have a datagrid (A C1 datagrid, in this case) bound to a property in my View Model. The XAML for the datagrid looks like this:
<c1:C1DataGrid
AutoGenerateColumns="False"
IsReadOnly="False"
Margin="5" Width="auto"
MinWidth="250"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
Name="dgNotifAssign"
CanUserAddRows="True"
ItemsSource="{Binding Path=notifCodeSubs.notification_configuration}"
>
<c1:C1DataGrid.Columns>
<c1:DataGridTextColumn
Binding="{Binding Path=user_id}"
Header="Recipient"
VerticalAlignment="Stretch"
SortMemberPath="user_id"
>
The property that it is bound to, in my viewmodel, looks like this:
Public Property notifCodeSubs As dsPeruseFM
Get
If _notifCode Is Nothing Then
_notifCode = New dsPeruseFM
End If
Return _notifCode
End Get
Set(ByVal value As dsPeruseFM)
MsgBox("If you can see this, success!")
End Set
End Property
In the codebehind I create an instance of the viewmodel and set the datacontext of the xaml to that instance, rather simple...
Dim vm As New ctrlAlertNotifyVM
As well as:
ctrlAlertNotifyXML.DataContext = vm
The above configuration compiles and reads data just fine. The grid is populated with all the correct data, etc. The problem comes when I try to add Mode=twoway to the ItemsSource on the datagrid. At that point VS2010 spits out the following error:
A TwoWay or OneWayToSource binding cannot work on the read-only property 'notification_configuration' of type 'PeruseFM.dsPeruseFM'.
I'm quite sure that all of my properties are read/write. And while the set command for this is nothing more than a message box at this point, it doesn't seem like I can even access that.
So the question is... has anybody ever encountered this issue before?
Update, response to question "What does notification_configuration look like?" from sixlettervariables:
Public Function codeChanged(Optional ByVal x As String = "")
If _notifCode Is Nothing Then
_notifCode = New dsPeruseFM
End If
taNotifSubs.fillNotifSubs(notifCode:=x, dataTable:=_notifCode.notification_configuration)
Return _notifCode
End Function
You've shown us that notifCodeSubs is read/write, however, that is not the actual property you've bound to.
From this viewpoint, the error message is fairly self-explanatory:
...read-only property 'notification_configuration'...
Therefore you cannot apply TwoWay binding to that property as an ItemsSource.
I have a custom control that has a dependency property that should fill a datagrid's ItemsSource property in the control. For some reason the collection never gets filled and the ItemsSource always ends up empty.
Public Shared ReadOnly ItemsSourceProperty As DependencyProperty = DependencyProperty.Register("ItemsSource", GetType(ObservableObjectCollection), GetType(HomePageControl), New PropertyMetadata(New ObservableObjectCollection, New PropertyChangedCallback(AddressOf ItemsSourceCallback)))
Public Property ItemsSource As ObservableObjectCollection
Get
Return DirectCast(GetValue(HomePageControl.ItemsSourceProperty), ObservableObjectCollection)
End Get
Set(value As ObservableObjectCollection)
SetValue(HomePageControl.ItemsSourceProperty, value)
End Set
End Property
Xaml:
<Controls:HomePageControl x:Name="myControl"
Margin="0,25,0,0"
Grid.Row="1"
HeaderText="{Binding Source={StaticResource MainViewModel}, Path=CurrentUser, Mode=TwoWay, UpdateSourceTrigger=Default}"
HeaderCount="30"
HeaderLinkText="This is optional."
HeaderLinkURI="/Projects"
ItemsSource="{Binding Source={StaticResource MainViewModel}, Path=TaskList, Mode=TwoWay, UpdateSourceTrigger=Default}"
GridViewStyle="{StaticResource RadGridViewStyleNoFlag}"
/>
I have five other DPs that work just fine, 3 strings and one style (to set up the columns of the datagrid) and a URI. I can either set them directly, as in "This is optional", or by binding them like the HeaderText, but the ItemsSource property never seems to get bound... a gridview outside of the control that uses the same binding as used in the ItemsSource binding inside the control gets filled... so the binding does return a filled collection, but it is not making it to the datagrid inside the custom control.
If I change ObservableObjectCollection to the object type that I'm using in the collection that I bind, then it works fine. The reason I want to use ObservableObjectCollection is because I want to be able to bind any object to the datagrid, just like a datagrid can... it doesn't care what type you bind to it - how can I get my user control to not care?
I've an WPF application where tried to implement MVVM pattern and Prism 2. I have a Usercontrol which has subscribed to an event fired from another Usercontrol. I would like to toggle visibility of few child elements in the subscribing control. Events are fired properly, even I am successfully able to bind data to some elements. How do I bind Visibility or any style property for that matter with the ViewModel and change them dynamically.
You can have a boolean property in your ViewModel and bind that property to the Visibility property of your controls. Since you will be asigning a boolean value and the Visibility property is expecting a Visibility enumeration value, you will have to use the BooleanToVisibilityConverter converter to make the conversion,
<Style.Resources>
<BooleanToVisibilityConverter x:Key="booleanToVisibilityConverter" />
</Style.Resources>
<Image Visibility="{Binding Path=ShowImage,
Converter={StaticResource booleanToVisibilityConverter}}"/>
Hope this helps.
Ezequiel Jadib
Although adding a Boolean property and using a value converter works, I would recommend adding a property of type Visibility to your ViewModel, e.g.
public Visibility ImageVisibility
{
get { return shouldShowImage ? Visibility.Visible : Visibility.Collapsed }
}
The advantage of this method is you don't need to write a converter for every property you want to express in a visual way (e.g. for a stock level that turns a label red when it drops below 10, you could have a converter you use once or just expose a StockLabelBrush property from your VM)
There's a simple solution for people who run into this issue.
In your view model, create a "Visibility" property like so:
public Visibility ShowModifyButtons
{
get { return (Visibility)GetValue(ShowModifyButtonsProperty); }
set { SetValue(ShowModifyButtonsProperty, value); }
}
public static readonly DependencyProperty ShowModifyButtonsProperty =
DependencyProperty.Register("ShowModifyButtons", typeof(Visibility), typeof(FileMatchViewModel),
new UIPropertyMetadata(Visibility.Collapsed));
In your XAML, bind to it like so:
<Button Focusable="False" Content="Save" Width="100" Margin="10" Visibility="{Binding ShowModifyButtons}"/>
Now, from your view model, you can set ShowModifyButtons to Visibility.Collapsed or Visibility.Visible as needed.
I understand that Silverlight 3.0 has binding but just want a simple example on how to use this to read a property from a class.
I have a class called Appointment which as a String property called Location:
Public Property Location() As String
Get
Return _Location
End Get
Set(ByVal Value As String)
_Location = Value
End Set
End Property
With a Private Declaration for the _Location as String of course.
I want a XAML element to bind to this property to display this in a TextElement, but it must be in XAML and not code, for example I want something like this:
<TextBlock Text="{Binding Appointment.Location}"/>
What do I need to do to get this to work?
It has to be a Silverlight 3.0 solution as some WPF features are not present such as DynamicResource which is what I'm used to using.
Just to add that my XAML is being loaded in from a seperate XAML File, this may be a factor in why the binding examples don't seem to work, as there are different XAML files the same Appointment.Location data needs to be applied.
You have two options.
If the "Appointment" class can be used as the DataContext for the control or Window, you can do:
<TextBlock Text="{Binding Location}" />
If, however, "Appointment" is a property of your current DataContext, you need a more complex path for the binding:
<TextBlock Text="{Binding Path=Appointment.Location}" />
Full details are documented in MSDN under the Binding Declarations page. If neither of these are working, make sure you have the DataContext set correctly.
You need something in code, unless you want to declare an instance of Appointment in a resource and bind to that but I doubt thats what you want.
You need to bind the Text property to the Property Path "Location" then assign the DataContext of the containing XAML to an instance of the Appointment:-
<Grid x:Name="LayoutRoot" Background="White">
<TextBlock Text="{Binding Location}" />
</Grid>
Then in the control's load event:-
void Page_Loaded(object sender, RoutedEventArgs e)
{
this.DataContext = new Appointment() { Location = "SomePlace" };
}
Note in this case I'm using the default Page control.
If I'm reading correctly, you need to create an instance of Appointment, set the DataContext of the control to that instance and modify your binding to just say: Text="{Binding Location}"
Also, consider implementing INotifyPropertyChanged on your Appointment class to allow the data classes to notify the UI of property value changes.