簡體   English   中英

如何綁定wpf UserControl?

[英]How to bind wpf UserControl?

我在將viewmodel綁定到我創建的usercontrol時遇到問題。 viewmodel不能反映值的變化。

UpDown.xaml(用戶控件xaml部分)btnUp和btnDown用於更改txtNum的文本。 不知道對不對

<Grid DataContext="{Binding ElementName=btnDownRoot}">
    <Grid.RowDefinitions>
        <RowDefinition Height="0*"/>
        <RowDefinition/>
        <RowDefinition Height="0*"/>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="0*"/>
        <ColumnDefinition Width="78*"/>
        <ColumnDefinition Width="104*"/>
        <ColumnDefinition Width="77*"/>
        <ColumnDefinition Width="0*"/>
    </Grid.ColumnDefinitions>
    <TextBox Name="txtNum" Text="{Binding ElementName=btnDownRoot, Path=NumValue}" FontSize="20" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" BorderBrush="White" Grid.Column="2" Grid.RowSpan="2"/>
    <Button Name="btnUp" Style="{StaticResource ResourceKey=btnUp}" Click="btnUp_Click" Grid.Column="3" Grid.RowSpan="2"/>
    <Button Name="btnDown" Style="{StaticResource ResourceKey=btnDown}" Click="btnDown_Click" Grid.RowSpan="2" Grid.ColumnSpan="2"/>
</Grid>

UpDown.xaml.cs(用戶控件代碼部分)

    public string NumValue
    {
        get
        {

            return GetValue(NumValueProperty).ToString();
        }
        set
        {

            SetValue(NumValueProperty, value);
        }
    }

    public static readonly DependencyProperty NumValueProperty =
        DependencyProperty.Register("NumValue", typeof(string), typeof(UpDown), 
            new PropertyMetadata("1", new PropertyChangedCallback(OnNumChanged)));

    private static void OnNumChanged(DependencyObject d,DependencyPropertyChangedEventArgs e)
    {
        var instannce = d.ToString();
    }

在MainWindow中,我將MainViewModel綁定到UserControl MainViewModel.cs

public class MainViewModel: BaseViewModel
{
    private string _myValue;
    public string MyValue
    {
        get { return _myValue; }
        set
        {
            _myValue = value;
            OnPropertyChanged("MyValue");

        }
    }
}

MainWindow.xaml

<Window.DataContext>
    <local:MainViewModel/>
</Window.DataContext>
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition/>
        <RowDefinition/>
    </Grid.RowDefinitions>
    <local:UpDown Grid.Row="0" NumValue="{Binding MyValue}" x:Name="udGuestNum"  Width="220"  Margin="0 20"/>
    <Button Name="btnOK" Grid.Row="1" Content="OK" Click="btnOK_Click"/>
</Grid>

默認情況下,NumValue綁定是OneWay。 要么將其明確設置為TwoWay

NumValue="{Binding MyValue, Mode=TwoWay}" 

或將TwoWay設為默認值:

public static readonly DependencyProperty NumValueProperty =
    DependencyProperty.Register(
        "NumValue", typeof(string), typeof(UpDown),
        new FrameworkPropertyMetadata(
            "1", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnNumChanged));

數字上/下控制已經開發出來...不要浪費時間,您甚至可以抓取代碼> https://github.com/xceedsoftware/wpftoolkit

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM