Click here to Skip to main content
15,897,718 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi
I have WPF user control which gives me exactly the similar functionaltity as the Hard Disk View (eg. C drive) when we opened the Windows Explorer. Likewise on top of Bar, I have 2 labels left top and right top positions. My WPF application have a ObservableCollection object contain, collection of 3 propertied object. (string LeftTopLabel, string rightTopLabel and int ProgressOffset). I placed this control in the ItemsControl and and when I tried to bind the properties by {Binding...}, it shows as it will not support. I need to declare all these properties as DependancyProperty and use it. I googled a lot and tried to solve it. But could not succeed.I know this is just a broad way of description, but waiting for your valid solution/suggestion.

For your reference I am pasting the latest tired code below.


---------------------------------------------------------------------------
User Control XAML file
---------------------------------------------------------------------------
HTML
<UserControl x:Class="DynamicLoading.TwoDBar"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="40" d:DesignWidth="250">
    <Grid x:Name="recGrid" Width="auto" >
        <Grid.RowDefinitions>
            <RowDefinition Height="auto" />
            <RowDefinition />
        </Grid.RowDefinitions>
        
        <Grid.ColumnDefinitions>
            <ColumnDefinition />
            <ColumnDefinition />
        </Grid.ColumnDefinitions>
        <TextBlock Grid.Row="0" Grid.Column="0" x:Name="lblLabel" Text= "{Binding LabelString, Mode=TwoWay}" HorizontalAlignment="Left" VerticalAlignment="Top"></TextBlock>
        <TextBlock Grid.Row="0" Grid.Column="1" x:Name="lblValue" Text="{Binding ValueString, Mode=TwoWay}" TextAlignment="Right"></TextBlock>
        <Rectangle Grid.Row="1" Grid.ColumnSpan="2" x:Name = "recBar" Width="auto" StrokeThickness="0.5" Stroke="Gray">
            <Rectangle.Fill>
                <LinearGradientBrush StartPoint="0,1" EndPoint="1,1"  >
                    <GradientStop x:Name="GRD1" Color="LightGray" Offset="{Binding OffsetValue, Mode=TwoWay}" />
                    <GradientStop x:Name="GRD2" Color="white" Offset="{Binding OffsetValue, Mode=TwoWay}"/>                    
                </LinearGradientBrush>
            </Rectangle.Fill>
        </Rectangle>        
    </Grid>
</UserControl>


---------------------------------------------------------------------------
User Control : Code Behid
---------------------------------------------------------------------------
public partial class TwoDBar : UserControl
{
public double OffsetValue
{
get { return (double)GetValue(OffsetProperty); }
set
{
SetValue(OffsetProperty, value);
}
}
public static readonly DependencyProperty OffsetProperty = DependencyProperty.Register("OffsetValue", typeof(double), typeof(TwoDBar), new FrameworkPropertyMetadata((double)0, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));

///
/// Array Name
///

public string LabelString
{
get { return (string)GetValue(LabelStringProperty); }
set
{
SetValue(LabelStringProperty, value);
}
}
public static readonly DependencyProperty LabelStringProperty = DependencyProperty.Register("LabelString", typeof(string), typeof(TwoDBar), new FrameworkPropertyMetadata((string)"Test1", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));

///
/// Total disc size
///

public string ValueString
{
get { return (string)GetValue(ValueStringProperty); }
set
{
SetValue(ValueStringProperty, value);
}
}
public static readonly DependencyProperty ValueStringProperty = DependencyProperty.Register("ValueString", typeof(string), typeof(TwoDBar), new FrameworkPropertyMetadata((string)"Test2", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));
}
}

---------------------------------------------------------------------------
In the MainWindow.XAML
---------------------------------------------------------------------------
<window xmlns:dynamicloading="clr-namespace:DynamicLoading" x:class="DynamicLoading.MainWindow" xmlns:x="#unknown">
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:DynamicLoading"
Title="MainWindow" Height="350" Width="525">

<scrollviewer verticalscrollbarvisibility="Auto">
<grid x:name="LayoutRoot">
<itemscontrol x:name="tbd" itemssource="{Binding}" margin="0,10,10,60">
<local:twodbar x:name="twoDBar" height="30" width="150" labelstring="{Binding SaName}" xmlns:local="#unknown">


Posted

1 solution

Not 100% sure I get your question. Are you refering to binding the MainWindow to the values in the usercontrol?

If so, you will need to add your usercontrol to the main window and give it a name (unique name / id). Once that is in place, your mainwindow elements can bind to the user control using element binding. ie.

XML
{Binding ElementName=usercontrolname, path=propertyname, mode=oneway/twoway}
etc.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900