简体   繁体   中英

How to bind wpf UserControl?

I am having a problem to binding viewmodel to my created usercontrol. The viewmodel doesn't reflect the value changes.

UpDown.xaml (Usercontrol xaml part) btnUp and btnDown are used to change the text of txtNum. I don't know whether it is right?

<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 (Usercontrol codepart)

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

In the MainWindow,I am going to bind the MainViewModel to the 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>

The NumValue Binding is OneWay by default. Either explicitly set it to TwoWay

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

or make TwoWay the default:

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

数字上/下控制已经开发出来...不要浪费时间,您甚至可以抓取代码> https://github.com/xceedsoftware/wpftoolkit

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM