使用ObservableCollection对象绑定WPF UserControl(WFF控件内部) [英] Binding WPF UserControl (WFF controls inside) with ObservableCollection object

查看:55
本文介绍了使用ObservableCollection对象绑定WPF UserControl(WFF控件内部)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我有WPF用户控件,当我们打开Windows资源管理器时,它给出了与硬盘视图(例如C盘)完全相同的功能。同样在Bar的顶部,我有两个标签,左上角和右上角位置。我的WPF应用程序有一个ObservableCollection对象,包含3个属性对象的集合。 (字符串LeftTopLabel,字符串rightTopLabel和int ProgressOffset)。我将此控件放在ItemsControl中,当我尝试通过{Binding ...}绑定属性时,它显示为不支持。我需要将所有这些属性声明为DependancyProperty并使用它。我google了很多,并试图解决它。但是不能成功。我知道这只是一种广泛的描述方式,但等待你的有效解决方案/建议。



供您参考我粘贴下面最新的疲劳代码。





------------------------------------------------ ---------------------------

用户控制XAML文件

--------------------------------------- ------------------------------------

 <   UserControl     x:Class   =  DynamicLoading.TwoDBar  

xmlns = http://schemas.microsoft.com/winfx/2006/xaml/presentation

xmlns:x = < span class =code-keyword> http://schemas.microsoft.com/winfx/2006/xaml

< span class =code-attribute> xmlns:mc = http://schemas.openxmlformats.org/ markup-compatibility / 2006

< span class =code-attribute> xmlns:d = http://schemas.microsoft.com/expression/blend/2008

mc:可忽略 = d

d:DesignHeight = 40 d:DesignWidth = 250 >
< 网格 x:名称 = recGrid 宽度 = auto >
< Grid.RowDefinitions >
< ; RowDefinition 高度 = auto / >
< RowDefinition / >
< /Grid.RowDefinitions >

< Grid.ColumnDefinitions >
< ColumnDefinition / >
< ColumnDefinition / >
< / Grid.ColumnDefinitions >
< TextBlock Grid.Row = 0 Grid.Column = 0 x:名称 = lblLabel 文字 = {Binding LabelString,Mode = TwoWay} Horizo​​ntalAlignment = VerticalAlignment = 热门 > < / TextBlock >
< TextBlock Grid.Row = 0 网格.Column = 1 x:名称 = lblValue 文字 = {Binding ValueString,Mode = TwoWay} TextAlignment = > < / TextBlock < span class =code-keyword>>

< 矩形 Grid.Row = 1 Grid.ColumnSpan = 2 x:名称 = recBar 宽度 = auto StrokeThickness = 0.5 笔划 = 灰色 >
< Rectangle.Fill >
< LinearGradientBrush StartPoint = 0,1 EndPoint = 1,1 >
< GradientStop x:Name = GRD1 < span class =code-attribute>颜色
= LightGray 偏移量 = {Binding OffsetValue,Mode = TwoWay} / >
< GradientStop x:名称 = GRD2 颜色 = white 偏移量 = {Binding OffsetValue, Mode = TwoWay} / >
< / LinearGradientBrush >
< / Rectangle.Fill >
< < span class =code-leadattribute> / Rectangle >
< / Grid >
< / UserControl >





------------------------------------------ ---------------------------------

用户控制:代码Behid

-------------------------------- -------------------------------------------

公共部分类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),新的FrameworkPropertyMetadata((double)0,FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));



///< summary>

///数组名称

///

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));



///< summary>

///总光盘大小

///

公共字符串ValueString

{

get {return(string)GetValue( ValueStringProperty); }

set

{

SetValue(ValueStringProperty,value);

}

}

public static readonly DependencyProperty ValueStringProperty = DependencyProperty.Register(ValueString,typeof(string),typeof(TwoDBar),新的FrameworkPropertyMetadata((字符串)Test2,FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));

}

}



---------------- -------------------------------------------------- ---------

在MainWindow.XAML

------ -------------------------------------------------- -------------------

< window xmlns:dynamicloading =clr-namespace:DynamicLoadingx: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高度=350宽度=525>



< scrollviewer verticalscrollbarvisibility =自动>

< grid x :name =LayoutRoot>

< itemscontrol x:name =tbditemssource ={Binding}margin =0,10,10,60>

< local:twodbar x:name =twoDBarheight =30widt h =150labelstring ={Binding SaName}xmlns:local =#unknown>




解决方案

不是100%肯定我得到你的问题。你是指将MainWindow绑定到usercontrol中的值吗?



如果是这样,你需要将你的usercontrol添加到主窗口并给它一个名字(唯一名称/ ID)。一旦到位,您的主窗口元素可以使用元素绑定绑定到用户控件。即。



 {Binding ElementName = usercontrolname,path = propertyname,mode = oneway / twoway} 


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
---------------------------------------------------------------------------

<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));

/// <summary>
/// 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));

/// <summary>
/// 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">


解决方案

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.

{Binding ElementName=usercontrolname, path=propertyname, mode=oneway/twoway}

etc.


这篇关于使用ObservableCollection对象绑定WPF UserControl(WFF控件内部)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆