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}" />
Related
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}"/>
How to change legend color in code behind in Silverlight:
<esri:Map x:Name="Map" Background="White" Loaded="Map_Loaded" WrapAround="True" Grid.ColumnSpan="3"
MouseClick="Map_MouseClick" >
<esri:ArcGISDynamicMapServiceLayer x:Name="gisdynamic" ID="gisdynamic"
Url="http://192.168.10.1:6080/arcgis/rest/services/babolsar/Babolsar10/MapServer"
DisplayName="نقشه بابلسر" DisableClientCaching="true" Initialized="Map_Initialized" InitializationFailed="layer_InitializationFailed"
DynamicLayerInfos="{x:Null}" ImageFormat="PNG24" LayerDrawingOptions="{x:Null}" ProxyURL="{x:Null}" />
<esri:GraphicsLayer ShowLegend="false" ID="ResultsGraphicsLayer" />
<esri:GraphicsLayer ShowLegend="false" ID="MyGraphicsLayer" />
<esri:GraphicsLayer ShowLegend="false" ID="MySelectionGraphicsLayer" MouseEnter="GraphicsLayer_MouseEnter" MouseLeave="GraphicsLayer_MouseLeave"/>
<esri:GraphicsLayer ShowLegend="false" ID="IdentifyIconGraphicsLayer"/>
<esri:Legend Name="Legend" HorizontalAlignment="Left" Background="White" Foreground="White"
`enter code here` VerticalAlignment="Top" Margin="0,0,0,0" Width="300"
Map="{Binding ElementName=Map}"
LayerItemsMode="Tree" ShowOnlyVisibleLayers="false" Refreshed="Legend_Refreshed">
<esri:Legend.MapLayerTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<CheckBox Content="{Binding Label}"
IsChecked="{Binding IsEnabled, Mode=TwoWay}"
IsEnabled="{Binding IsInScaleRange}" >
</CheckBox>
<Slider Maximum="1" Value="{Binding Layer.Opacity, Mode=TwoWay}" Width="100" />
</StackPanel>
</DataTemplate>
</esri:Legend.MapLayerTemplate>
<esri:Legend.LayerTemplate>
<DataTemplate>
<CheckBox Content="{Binding Label}"
IsChecked="{Binding IsEnabled, Mode=TwoWay}"
IsEnabled="{Binding IsInScaleRange}" >
</CheckBox>
</DataTemplate>
</esri:Legend.LayerTemplate>
</esri:Legend>
Regarding Legend Color and font changing the following links will help you ..you just visit it
http://support.esri.com/es/knowledgebase/techarticles/detail/38761
https://geonet.esri.com/thread/97018
Based on a selected string value in the combobox I want to either show red/blue datatempalte inside the grid.
Can this be done without a ContentControl?
<UserControl.Resources >
<DataTemplate x:Key="red">
<TextBox Text="red" />
</DataTemplate>
<DataTemplate x:Key="blue">
<TextBox Text="blue" />
</DataTemplate>
</UserControl.Resources>
<ComboBox ??? />
<Grid>
// Show red or blue datatemplate here
</Grid>
Why not use a ContentControl?
To make this work simply i would put the templates in an array, which the ComboBox then can bind to:
<x:Array x:Key="templates" Type="{x:Type DataTemplate}">
<DataTemplate>
<DataTemplate.Resources>
<sys:String x:Key="DisplayString">Red</sys:String>
</DataTemplate.Resources>
<TextBox Text="red" />
</DataTemplate>
<DataTemplate>
<DataTemplate.Resources>
<sys:String x:Key="DisplayString">Blue</sys:String>
</DataTemplate.Resources>
<TextBox Text="blue" />
</DataTemplate>
</x:Array>
<ComboBox Name="combo" ItemsSource="{StaticResource templates}">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Resources[DisplayString]}" />
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
<Grid>
<ContentControl ContentTemplate="{Binding SelectedItem, ElementName=combo}" />
</Grid>
I have a ListBox like this :
<ListBox DataContext="{Binding UpdateSourceTrigger=PropertyChanged}"
ItemsSource="{Binding UpdateSourceTrigger=PropertyChanged}"
ListBoxItem.Selected="ListBoxItem_Selected">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<DockPanel>
<Label Content="{Binding Path=Attribute[rdv].Value, UpdateSourceTrigger=PropertyChanged}" />
</DockPanel>
<DockPanel>
<Label Content="{Binding Path=Attribute[type].Value, UpdateSourceTrigger=PropertyChanged}" />
<Label Content="{Binding Path=Element[ville].Attribute[saisie].Value, UpdateSourceTrigger=PropertyChanged}" />
<Label Content=":" />
<Label Content="{Binding Path=Element[adresse].Attribute[saisie].Value, UpdateSourceTrigger=PropertyChanged}" />
</DockPanel>
<Separator />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
I whant to rise an event when an ListeBoxItem is selected.
As you can see, I've tried with ListBoxItem.Selected="ListBoxItem_Selected" but it does not work.
Do you have an idea ?
Tanks by advance !
You handler doesn't get called because the Selected event is already handled by the ListBox. You should handle the SelectionChanged event in the ListBox instead:
<ListBox DataContext="{Binding UpdateSourceTrigger=PropertyChanged}"
ItemsSource="{Binding UpdateSourceTrigger=PropertyChanged}"
SelectionChanged="ListBox_SelectionChanged">
Alternatively, you can use an ItemContainerStyle to attach the handler to every ListBoxItem:
<ListBox DataContext="{Binding UpdateSourceTrigger=PropertyChanged}"
ItemsSource="{Binding UpdateSourceTrigger=PropertyChanged}">
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<EventSetter Event="Selected" Handler="ListBoxItem_Selected"/>
</Style>
</ListBox.ItemContainerStyle>
I want to put a command on a ListBoxItem. The ListBoxItem use a DataTemplate composed of a StackPanel (containing an Image and a TextBlock, both using Binding). I want that the doubleclick on that ListBoxItem fire the command.
I have tried this :
<DataTemplate>
<StackPanel>
<StackPanel.Resources>
<CommonUI:CommandReference x:Key="DoubleClickCommand" Command="{Binding Path=DefaultCommand}" />
</StackPanel.Resources>
<StackPanel.InputBindings>
<MouseBinding Gesture="LeftDoubleClick" Command="{StaticResource DoubleClickCommand}" />
</StackPanel.InputBindings>
<Image Source="{Binding Path=Thumbnail, IsAsync=True}" IsHitTestVisible="False"/>
<TextBlock Text="{Binding Path=Name}" IsHitTestVisible="False">
</StackPanel>
</DataTemplate>
I have also tried to put the Command Resources on a StackPanel containing this StackPanel, without any change.
I am certain of my binding because when I put the InputBindings part on the TextBlock, it works.
Thanks
Try handling the event in the ListBox instead of the StackPanel:
<ListBox>
<ListBox.Resources>
<CommonUI:CommandReference x:Key="DoubleClickCommand" Command="{Binding Path=DefaultCommand}" />
</ListBox.Resources>
<ListBox.InputBindings>
<MouseBinding Gesture="LeftDoubleClick" Command="{StaticResource DoubleClickCommand}" />
</ListBox.InputBindings>
<DataTemplate>
<StackPanel>
<Image Source="{Binding Path=Thumbnail, IsAsync=True}" />
<TextBlock Text="{Binding Path=Name}" />
</StackPanel>
</DataTemplate>
</ListBox>
My code finally looks like this :
<DataTemplate>
<StackPanel Orientation="Vertical">
<StackPanel.Resources>
<CommonUI:CommandReference x:Key="DoubleClickCommand" Command="{Binding Path=DefaultCommand}" />
</StackPanel.Resources>
<StackPanel.InputBindings>
<MouseBinding Gesture="LeftDoubleClick" Command="{StaticResource DoubleClickCommand}" />
</StackPanel.InputBindings>
<Image Source="{Binding Path=Thumbnail, IsAsync=True}" />
<TextBlock Text="{Binding Path=Name}" />
</StackPanel>
</DataTemplate>
Thanks anyway, Mr Poulin.