ListBoxEditItem Visibility not working as expected - wpf

I have the following xaml and I am trying to set the ListBoxEditItem for Content="3" without using the ElementName for ck because I want to set the Visibility directly and not have the CheckBox. Why does the Visibility work for Content="2" ListBoxEditItem and not Content="3"? How do I set the Visibility directly?
<StackPanel>
<dxe:ComboBoxEdit EditValue="{Binding test, UpdateSourceTrigger=PropertyChanged}">
<Visibility>Visible</Visibility>
<Visibility>Collapsed</Visibility>
<Visibility>Hidden</Visibility>
</dxe:ComboBoxEdit>
<dxe:ListBoxEdit StyleSettings="{dxe:RadioListBoxEditStyleSettings}">
<dxe:ListBoxEditItem Content="1" />
<dxe:ListBoxEditItem Content="2" Visibility="{Binding Visibility, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, ElementName=ck}" />
<dxe:ListBoxEditItem Content="3" Visibility="{Binding test, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
</dxe:ListBoxEdit>
<CheckBox x:Name="ck" Visibility="{Binding test, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
<TextBlock Text="some stuff" Visibility="{Binding Visibility, ElementName=ck}" />
</StackPanel>

I needed DataContext in the binding like so:
<dxe:ListBoxEditItem Content="3" Visibility="{Binding DataContext.test, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>

Related

Binding an image in a datatemplate

I'm trying to add a menu to my application, I'd like that my menu header is the currently connected user's picture.
My issue is that every binding works except the one for the image, from what I've understood on the net the DataTemplate I use to replace the menu header by a picture doesn't have access to it's parent Datacontext. I tried to use some stuff like relative source and other tricks but with no luck (certainely related to the fact that I'm new in mvvm and I obviously still have a lot to learn :) )
Here's the code I'm using :
<Grid DataContext="{Binding User}">
<DockPanel>
<Label Content="{Binding Strings.Hello, Source={StaticResource StringLocalizer} }" VerticalAlignment="Center" Padding="0,0,5,0" FontFamily="{DynamicResource Font_Normal}" FontSize="15" Foreground="White"/>
<TextBlock Text="{Binding DisplayName, TargetNullValue='Guest', FallbackValue='Guest'}" VerticalAlignment="Center" HorizontalAlignment="Left" FontFamily="{DynamicResource Font_Normal}" FontSize="15" Foreground="White"/>
<Menu Name="LogonMenu" Margin="10,0,0,0" DataContext="{Binding User}">
<MenuItem >
<MenuItem.HeaderTemplate>
<DataTemplate>
<!--<Image Source="{Binding Datacontext.Image,RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Application}}, Converter={StaticResource ImageConverter}, FallbackValue={StaticResource DefaultUserIcon}}"/>-->
<Image DataContext="{Binding User}" Source="{Binding Path=Image, Converter={StaticResource ImageConverter}, FallbackValue={StaticResource DefaultUserIcon},Mode=TwoWay}"/>
</DataTemplate>
</MenuItem.HeaderTemplate>
<MenuItem Header="{Binding Strings.SignIn, Source={StaticResource StringLocalizer}}" Width="150" Margin="10,0,0,0" IsEnabled="{Binding Connected, Converter={StaticResource BoolInverterConverter}}" Visibility="{Binding RelativeSource={RelativeSource Mode=Self},Path=IsEnabled, Converter={StaticResource BoolToVisibilityConverter}}" Command="{Binding RelativeSource={RelativeSource AncestorType=Window},Path=DataContext.SignInCommand}"/>
<MenuItem Header="{Binding Strings.SignOut, Source={StaticResource StringLocalizer}}" Margin="10,0,0,0" IsEnabled="{Binding Connected}" Visibility="{Binding RelativeSource={RelativeSource Mode=Self},Path=IsEnabled, Converter={StaticResource BoolToVisibilityConverter}}" Command="{Binding RelativeSource={RelativeSource AncestorType=Window},Path=DataContext.SignOutCommand}"/>
<MenuItem Header="{Binding Strings.UserInformation, Source={StaticResource StringLocalizer}}" Margin="10,0,0,0" IsEnabled="{Binding Connected}" Visibility="{Binding RelativeSource={RelativeSource Mode=Self},Path=IsEnabled, Converter={StaticResource BoolToVisibilityConverter}}"/>
</MenuItem>
</Menu>
</DockPanel>
</Grid>
In my case the image always shows the fallback picture, if someone could help me finding a way to have my user's picture it would be great.

WPF Element Binding not working in XAML

This should be fairly simple and straightforward but element binding is not working in XAML when using it from resource. It is working fine when using it directly in XAML.
Resources:
<Window.Resources>
<StackPanel x:Key="panel">
<CheckBox x:Name="chkDefaultValue" Content="Default Value"
IsChecked="{Binding ElementName=txtDefaultValue, Path=Text.Length, Mode=OneWay}" />
<TextBox x:Name="txtDefaultValue"
Text="{Binding DefaultValue, Mode=TwoWay, ValidatesOnDataErrors=True}"
IsEnabled="{Binding ElementName=chkDefaultValue, Path=IsChecked}" />
</StackPanel>
</Window.Resources>
XAML:
<StackPanel>
<!-- BINDING NOT WORKING -->
<ContentControl Content="{StaticResource panel}" />
<!-- BINDING WORKING HERE -->
<CheckBox x:Name="chkDefaultValue" Content="Default Value"
IsChecked="{Binding ElementName=txtDefaultValue, Path=Text.Length, Mode=OneWay}" />
<TextBox x:Name="txtDefaultValue"
Text="{Binding DefaultValue, Mode=TwoWay, ValidatesOnDataErrors=True}"
IsEnabled="{Binding ElementName=chkDefaultValue, Path=IsChecked}" />
</StackPanel>
How could i fix it?
You should use DataTemplate
<Window.Resources>
<DataTemplate DataType="{x:Type ContentControl}" x:Key="panel">
<StackPanel>
<CheckBox x:Name="chkDefaultValue" Content="Default Value"
IsChecked="{Binding ElementName=txtDefaultValue, Path=Text.Length, Mode=OneWay}" />
<TextBox x:Name="txtDefaultValue"
Text="{Binding DefaultValue, Mode=TwoWay, ValidatesOnDataErrors=True}"
IsEnabled="{Binding ElementName=chkDefaultValue, Path=IsChecked}" />
</StackPanel>
</DataTemplate>
</Window.Resources>
and
<ContentControl ContentTemplate="{StaticResource panel}" />
didn't check, but probably works
And you can use ControlTemplate
<Window.Resources>
<ControlTemplate x:Key="panel">
<StackPanel>
<CheckBox x:Name="chkDefaultValue"
Content="Default Value"
IsChecked="{Binding ElementName=txtDefaultValue,
Path=Text.Length,
Mode=OneWay}" />
<TextBox x:Name="txtDefaultValue"
IsEnabled="{Binding ElementName=chkDefaultValue,
Path=IsChecked}"
Text="{Binding DefaultValue,
Mode=TwoWay,
ValidatesOnDataErrors=True}" />
</StackPanel>
</ControlTemplate>
</Window.Resources>
and
<ContentControl Template="{StaticResource panel}" />

Make default visibility collapsed when there is no binding element

I have a user control with textbox and button which are bound to properties from viewmodel.
<Grid>
<StackPanel>
<TextBox Text=" Hi" IsEnabled="{Binding IsReadOnly, UpdateSourceTrigger=PropertyChanged}"></TextBox>
<Button Content="B" Visibility="{Binding IsVisible, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource BooleanToVisibilityConverter}}"></Button>
</StackPanel>
</Grid>
How to make the button invisible when the datacontext is not provided for this usercontrol?
FallbackValue
<Button Content="B" Visibility="{Binding IsVisible,
UpdateSourceTrigger=PropertyChanged,
Converter={StaticResource BooleanToVisibilityConverter}},
FallbackValue=Collapsed"></Button>

How to use converters without binding paths?

I have a Combobox whose ItemsSource is an ObservableCollection of int values.
My combobox itemtemplate consists on an image and a textblock which content is given by 2 converters.
How can I set this 2 bindings? The following code does not compile:
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Image Source="{Binding, Converter={StaticResource IntToImageConverter}}" Stretch="None" />
<TextBlock Text="{Binding, Converter={StaticResource IntToStringConverter}}" />
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
You need to remove the , so:
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Image Source="{Binding Converter={StaticResource IntToImageConverter}}" Stretch="None" />
<TextBlock Text="{Binding Converter={StaticResource IntToStringConverter}}" />
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>

Can't bind child collection in silverlight

I have a pretty simple setup that I cannot get to work in silverlight. I have an order with a collection of OrderPayments. These objects are part of a Entity Framework model and are exposed through WCF RIA Services. I can bind perfectly fine to any basic property on the Order class, but I wanted to bind to a listbox to show the OrderPayments. Here's the XAML for the ListBox.
<ListBox ItemsSource="{Binding Data.OrderPayments, ElementName=orderDataSource}"></ListBox>
Nothing ever appears in the listbox and there is at least one OrderPayment for the order displayed. orderDataSource is a DomainDataSource that contains the Order. The odd thing about this is that I don't receive any binding errors and when I bind to a TextBlock using the following code:
<TextBlock Text="{Binding Data.OrderPayments, ElementName=orderDataSource}" />
The text 'OrderPayment' is outputted to the screen. Is there something different I have to do to get Silverlight to actually pick up the reference to the object?
Entire XAML below:
<riaControls:DomainDataSource AutoLoad="True" Name="orderDataSource" QueryName="GetOrder">
<riaControls:DomainDataSource.QueryParameters>
<riaControls:Parameter ParameterName="orderid" Value="1" />
</riaControls:DomainDataSource.QueryParameters>
<riaControls:DomainDataSource.DomainContext>
<ds:CEWCPSDomainContext />
</riaControls:DomainDataSource.DomainContext>
</riaControls:DomainDataSource>
<StackPanel Orientation="Vertical" Margin="12">
<StackPanel Orientation="Horizontal">
<Button Width="100" Height="50" Content="Save & Return" Margin="0,0,12,12" />
<Button Width="100" Height="50" Content="Orders" Margin="0,0,12,12" />
<Button Width="100" Height="50" Content="Emails" Margin="0,0,12,12" />
</StackPanel>
<StackPanel Orientation="Horizontal">
<dataForm:DataForm x:Name="dataForm1" Header="Order Contact Information"
AutoGenerateFields="False" AutoEdit="False" AutoCommit="False"
CurrentItem="{Binding Data, ElementName=orderDataSource}">
<dataForm:DataForm.EditTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<StackPanel>
<dataForm:DataField Label="First Name">
<TextBox Text="{Binding FirstName, Mode=TwoWay, NotifyOnValidationError=True, ValidatesOnExceptions=True }" />
</dataForm:DataField>
<dataForm:DataField Label="Last Name">
<TextBox Text="{Binding LastName, Mode=TwoWay, NotifyOnValidationError=True, ValidatesOnExceptions=True }"/>
</dataForm:DataField>
<dataForm:DataField Label="Organization">
<TextBox Text="{Binding Organization, Mode=TwoWay, NotifyOnValidationError=True, ValidatesOnExceptions=True }"/>
</dataForm:DataField>
<dataForm:DataField Label="Phone">
<TextBox Text="{Binding Phone, Mode=TwoWay, NotifyOnValidationError=True, ValidatesOnExceptions=True }"/>
</dataForm:DataField>
<dataForm:DataField Label="Fax">
<TextBox Text="{Binding Fax, Mode=TwoWay, NotifyOnValidationError=True, ValidatesOnExceptions=True }"/>
</dataForm:DataField>
<dataForm:DataField Label="Email">
<TextBox Text="{Binding Email, Mode=TwoWay, NotifyOnValidationError=True, ValidatesOnExceptions=True }"/>
</dataForm:DataField>
<dataForm:DataField Label="Address 1">
<TextBox Text="{Binding Address1, Mode=TwoWay, NotifyOnValidationError=True, ValidatesOnExceptions=True }"/>
</dataForm:DataField>
<dataForm:DataField Label="Address 2">
<TextBox Text="{Binding Address2, Mode=TwoWay, NotifyOnValidationError=True, ValidatesOnExceptions=True }"/>
</dataForm:DataField>
<dataForm:DataField Label="City">
<TextBox Text="{Binding City, Mode=TwoWay, NotifyOnValidationError=True, ValidatesOnExceptions=True }"/>
</dataForm:DataField>
<dataForm:DataField Label="State">
<TextBox Text="{Binding State, Mode=TwoWay, NotifyOnValidationError=True, ValidatesOnExceptions=True }"/>
</dataForm:DataField>
<StackPanel Orientation="Horizontal" Width="Auto">
<dataForm:DataField Label="Zip code">
<TextBox Text="{Binding Zip, Mode=TwoWay, NotifyOnValidationError=True, ValidatesOnExceptions=True }"/>
</dataForm:DataField>
<dataForm:DataField>
<TextBox Text="{Binding Zip4, Mode=TwoWay, NotifyOnValidationError=True, ValidatesOnExceptions=True }"/>
</dataForm:DataField>
</StackPanel>
<dataForm:DataField Label="Country">
<TextBox Text="{Binding Country, Mode=TwoWay, NotifyOnValidationError=True, ValidatesOnExceptions=True }"/>
</dataForm:DataField>
</StackPanel>
</StackPanel>
</DataTemplate>
</dataForm:DataForm.EditTemplate>
</dataForm:DataForm>
<StackPanel Orientation="Vertical">
<TextBlock FontWeight="Bold" FontSize="16" Text="Order Total / Payments" />
<TextBlock >
<Run Text="Order Total:" />
<Run Text="{Binding Data.OrderTotal, ElementName=orderDataSource}" />
</TextBlock>
<TextBlock Text="Payments" />
<TextBlock Text="{Binding Data.OrderPaymentItems, ElementName=orderDataSource}" />
<ListBox ItemsSource="{Binding Data.OrderPayments, ElementName=orderDataSource}">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Amount}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
</StackPanel>
</StackPanel>
WCF RIA Services does not include child entities by default. I needed to put [Include()] on the OrderPayments property of the Order object. Works like a charm now.

Resources